C++ 错误“未定义对 Class::Function() 的引用 [重复]

C++ error 'Undefined reference to Class::Function() [duplicate]

提问人:Ishan Rana 提问时间:4/28/2021 最后编辑:Ishan Rana 更新时间:4/28/2021 访问量:281

问:

我只是想知道是否有人可以帮助我解决这个问题,我是 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;
}

9月,

#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++ OOP 定义的 头文件 未定义引用

评论

0赞 acraig5075 4/28/2021
不执行 .cpp 或 .h 文件,而是将 c++ 文件编译为可执行文件并执行
0赞 IWonderWhatThisAPIDoes 4/28/2021
你这么说,就像你单独编译每个文件并尝试执行它一样。然后 - 当然 - SepMain.cpp 不知道构造函数定义,并且 Sep.cpp 没有主函数。您必须编译这两个文件并将它们链接在一起以形成实际的应用程序。你的编译器应该能够做到这一点
0赞 Ishan Rana 4/28/2021
@IWonderWhatThisAPIDoes您能知道如何编译这两个文件并将它们链接在一起以形成一个实际的应用程序
0赞 IWonderWhatThisAPIDoes 4/28/2021
不幸的是,这些东西是依赖于编译器的。您使用的是什么编译器?
0赞 Ishan Rana 4/28/2021
@IWonderWhatThisAPIDoes 编译器是 gcc 版本 9.3.0,我在 ubuntu 中使用它

答: 暂无答案