gpt4 book ai didi

c++ - 多重重定义错误

转载 作者:搜寻专家 更新时间:2023-10-30 23:49:35 24 4
gpt4 key购买 nike

在学习了更多关于类和指针的知识之后,我重构了一个程序并删除了超过 200 行代码,在这个过程中创建了另外两个类,LocationPiece。问题是,在编译所有内容后,链接器提示 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 的文件(好吧,其他文件间接地从 location 包含 piece.h 包含 piece.h),board.h 是唯一包含 location 的文件。 h,还有一堆类包括board.h

我尝试将 header guard 更改为 _OTHELLO_PIECE_H,并尝试将类重命名为 OPiece(通过 IDE)。都没有解决问题。

有趣的是,其中一个错误有一个“in function 'OPiece':”,之后我的 IDE 放了 chatter.o,即使 chatter.h 和 chatter.cpp 都没有包括任何包含 OPiece 的东西。

知道是什么导致了这个重定义错误吗?

最佳答案

  1. 重建一切
  2. 查找 Piece::Piece,并从头文件中删除所有此类2b.从不 #include .cpp 文件
  3. 寻找解析为 Piece 的 #define

关于c++ - 多重重定义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4054357/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com