我的编译器发出的以下错误:未定义的引用从类方法全局重载操作
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是的,这确实是一种风俗。我刚刚更新了我的问题。 –