我正在處理我的項目,而我決定將其分解爲文件。然而,我遇到了像這樣的問題,我通過谷歌發現的所有建議都是關於忘記連接兩個對象文件,我正在做對(至少我是這麼認爲的)。在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