2012-11-29 25 views
0

我的編譯器發出的以下錯誤:未定義的引用從類方法全局重載操作

matrix.o: In function `Matrix::modify_cell(unsigned int, unsigned int, int)': 
Matrix.cpp:(.text+0x5f): undefined reference to `operator!(Dim)' 
Matrix.cpp:(.text+0xa3): undefined reference to `operator!(Dim)' 
Matrix.cpp:(.text+0x178): undefined reference to `operator!(Dim)' 
Matrix.cpp:(.text+0x1a0): undefined reference to `operator!(Dim)' 
matrix.o: In function `List::nula(Dim) const': 
Matrix.cpp:(.text._ZNK4List4nulaE3Dim[List::nula(Dim) const]+0x11): undefined reference to `operator!(Dim)' 
list1.o:List - auxiliary methods.cpp:(.text+0x3b): more undefined references to `operator!(Dim)' follow 
collect2: ld returned 1 exit status 
make: *** [app] Error 1 

基質放置在文件Matrix.h和Matrix.cpp從類List繼承的是,在一類轉到放在List.h和另外兩個.cpp文件中。鍵入Dim(typedef)和運算符!因爲它是在包含在List.h和操作符中的aux.h中全局聲明的(在任何類之外的)!在aux.cpp中定義。在Matrix.h中,我包含List.h,所以我不明白是什麼問題。 aux.cpp實際上是在我的makefile中編譯爲.o,然後加入到單個應用程序中。我知道最好將我的typedef Dim和它的重載操作符放到一個類中,但它在類List和類Matrix中同時使用,而typedefs不是繼承的,我不知道任何其他解決方法。

編輯:

// aux.cpp 
#include "aux.h" 
Dim operator!(Dim dim) { return dim == COL ? ROW : COL; } 

// aux.h 
#ifndef AUX_H 
#define AUX_H 

/* (...) */ 

typedef enum { ROW, COL } Dim; 
inline Dim operator!(Dim dim); 

#endif 
+0

我們可以看到'operator!(Dim)'的定義嗎? – andre

+0

確保'operator!'的聲明和定義相同。 – chris

+0

@ahenderson是的,這確實是一種風俗。我剛剛更新了我的問題。 –

回答

1

您已經聲明operator!(Dim)爲內聯,所以它不可,除非你還包括在.H定義。要麼移動定義,要麼取出inline

+0

謝謝,那工作。但我現在有點困惑。我的唯一目標是把運營商!定義在一個單獨的文件中是,當它被放置在List.h中時,編譯器抱怨它的多重定義(因爲List.h被多次包含)。現在它起作用了,但在我看來它應該再次是運營商的多重定義的情況! (每次包含標題)。你能解釋給我嗎? –

+0

@infoholic_anonymous,你需要頭部守衛,以確保如果一個文件包含多次內容只能看到一次。 –

+0

但我確實在#ifndef #define的每個頭文件之前,並且以#endif指令結束。 –