首页 > 解决方案 > C++ 错误'未定义的对 Class::Function() 的引用

问题描述

我只是想知道是否有人可以帮助我解决这个问题,我是 C++ 新手,当我将类分成不同的文件时遇到问题。

编译 SepMain.cpp 文件时出现的错误是:

/usr/bin/ld: /tmp/cciHobHn.o: in function `main':
SepMain.cpp:(.text+0x23): undefined reference to `Sep::Sep()'
collect2: error: ld returned 1 exit status

编译 Sep.cpp 文件时出现的错误是:

/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x24): undefined reference to `main'
collect2: error: ld returned 1 exit status

编译 Sep.h 文件时出现的错误是:

bash: /home/ishan/Coding/C++/Learn/tempCodeRunnerFile: Permission denied

所以我的代码有三个文件:

九月.cpp

#include "Sep.h"        
#include <iostream>

using namespace std;   
Sep::Sep()
{
    cout<<"hello"<<endl;
}

九月

#ifndef SEP_H
#define SEP_H

class Sep
{
    public:
        Sep();
};

#endif

SepMain.cpp

#include "Sep.h"
#include <iostream>

using namespace std;

int main()
{
    Sep ir;
    return 0;
}

标签: c++oopundefinedheader-filesundefined-reference

解决方案


推荐阅读