在了解了更多关于类和指针的知识后,我重构了一个我拥有的程序并删除了超过 200 行代码,在此过程中创建了另外两个类Location
和Piece
. 问题是,在编译完所有内容后,链接器抱怨构造函数Piece
被定义了多次,并带有大量错误:
In function 'Piece': board.o
multiple definition of 'Piece::Piece(int)` char_traits.h
In function 'Piece': board.o
multiple definition of 'Piece::Piece(int)` piece.cpp
In function 'Piece': player.o
multiple definition of 'Piece::Piece(int)` piece.cpp
In function 'Piece': player.o
multiple definition of 'Piece::Piece(int)` piece.cpp (yes, same exact error!)
In function 'Piece': refereee.o
multiple definition of 'Piece::Piece(int)` char_traits.h
In function 'Piece': referee.o
multiple definition of 'Piece::Piece(int)` piece.cpp
...
当我单击 的错误时char_traits.h
,它会将我带到这里:
static size_t
length(const char_type* __s) //error points here to line 262
{ return __builtin_strlen(__s); }
另一个char_traits.h
把我带到
static int
compare(const char_type* __s1, const char_type* __s2, size_t __n) //line 258, error points here too
{ return __builtin_memcmp(__s1, __s2, __n); }
如您所知,location.h 是唯一包含piece.h 的文件(嗯,其他文件包括piece.h 间接来自包括piece.h 的位置),board.h 是唯一包含location.h 的文件,并且一堆类包括board.h
我尝试将标头保护更改为_OTHELLO_PIECE_H
,并尝试将类重命名为 OPiece(通过 IDE)。都没有解决问题。
有趣的是,其中一个错误有一个“in function 'OPiece':”,然后我的 IDE chatter.o
puts
知道可能导致此重新定义错误的原因吗?