2012-11-29 21 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指令结束。 –