我似乎无法让错误消失。错误如下。我看过谷歌搜索,但我仍然无法弄清楚。这不像我是 C++ 的新手,但我已经有一段时间没有玩弄它了。
奇怪的是它在 Windows 上与 g++ 一起工作……
使用错误:
g++ main.cpp
输出:
/tmp/ccJL2ZHE.o: 在函数
main': \ main.cpp:(.text+0x11): undefined reference to
Help::Help()’main.cpp:(.text+0x1d): undefined reference to
Help::sayName()' \ main.cpp:(.text+0x2e): undefined reference to
Help::~Help()’main.cpp:(.text+0x46): undefined reference to `Help::~Help()’
collect2: ld 返回 1 个退出状态
文件 main.cpp
#include <iostream>
#include "Help.h"
using namespace std;
int main () {
Help h;
h.sayName();
// ***
// ***
// ***
return 0;
}
文件 帮助.h
#ifndef HELP_H
#define HELP_H
class Help {
public:
Help();
~Help();
void sayName();
protected:
private:
};
#endif // HELP_H
文件 帮助.cpp
#include <iostream>
#include "Help.h"
using namespace std;
Help::Help() { // Constructor
}
Help::~Help() { // Destructor
}
void Help::sayName() {
cout << " ***************" << endl;
cout << " ************************************" << endl;
cout << " ************" << endl;
cout << " *********************" << endl;
}
原文由 Zeveso 发布,翻译遵循 CC BY-SA 4.0 许可协议
利用
您必须告诉编译器您希望它编译的所有文件,而不仅仅是第一个文件。