我的編譯器發出的以下錯誤:未定義的引用從類方法全局重載操作
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
我們可以看到'operator!(Dim)'的定義嗎? – andre
確保'operator!'的聲明和定義相同。 – chris
@ahenderson是的,這確實是一種風俗。我剛剛更新了我的問題。 –