【发布时间】:2011-05-02 13:07:09
【问题描述】:
在了解了类和指针的更多信息后,我重构了一个程序并删除了超过 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 间接来自location,包括piece.h),board.h 是唯一包含location 的文件。 h,还有一堆类包括 board.h
我尝试将标头保护更改为_OTHELLO_PIECE_H,并尝试将类重命名为 OPiece(通过 IDE)。都没有解决问题。
有趣的是,其中一个错误有一个“in function 'OPiece':”,然后我的 IDE 放置了chatter.o,即使 chatter.h 和 chatter.cpp 都不包含任何包含 OPiece 的内容。
知道什么可能导致这个重新定义错误吗?
【问题讨论】:
-
您尝试重建整个项目吗?关于 OPiece 的错误消息可能是由过时的预编译头引起的。
标签: c++ class header redefinition