我正在处理我的项目,而我决定将其分解为文件。然而,我遇到了像这样的问题,我通过谷歌发现的所有建议都是关于忘记连接两个对象文件,我正在做对(至少我是这么认为的)。在C++中包含标题时未定义的引用
生成文件:
test : class.o main.o
g++ class.o main.o -o test.exe
main.o : main.cpp
g++ main.cpp -c
class.o : class.cpp
g++ class.cpp -c
的main.cpp
#include <iostream>
#include "class.h"
using namespace std;
int main() {
Trida * t = new Trida(4);
t->fce();
return 0;
}
class.h
#ifndef CLASS
#define CLASS
class Trida {
private:
int a;
public:
Trida(int n);
void fce();
};
#endif
class.cpp
#include <iostream>
using namespace std;
class Trida {
private:
int a;
public:
Trida(int n) {
this->a = n;
}
void fce() {
cout << this->a << endl;
}
};
错误消息:
[email protected]:~/Skola/test$ make
g++ class.cpp -c
g++ main.cpp -c
g++ class.o main.o -o test.exe
main.o: In function `main':
main.cpp:(.text+0x26): undefined reference to `Trida::Trida(int)'
main.cpp:(.text+0x54): undefined reference to `Trida::fce()'
collect2: ld returned 1 exit status
make: *** [test] Error 1
哇,这是快。问题比我想象的要简单。非常感谢你 :-) – Gwynbleidd 2011-01-08 05:28:18