【问题标题】:Multiple redefinition error多重重定义错误
【发布时间】:2011-05-02 13:07:09
【问题描述】:

在了解了类和指针的更多信息后,我重构了一个程序并删除了超过 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 的文件(其他文件包括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


【解决方案1】:

在典型的构建中有两个地方可以实现Piece::Piece(int)

1) 接口(声明时)

class Piece {
    int d_a;
public:
    Piece(int a) : d_a(a) {
    }
    /* ... */
};

2) 在专用的 cpp 文件中

在 Piece.hpp 中

class Piece {
    int d_a;
public:
    Piece(int a);
    /* ... */
};

在 Piece.cpp 中

Piece::Piece(int a) : d_a(a) {
}

但是,模板的定义不同。

该错误通常表明Piece::Piece(int a) : d_a(a) {}包含在多个cpp文件中。

生成的每个目标文件都会在可见的地方添加符号Piece::Piece(int)

在构建结束时,所有对象都被合并/链接以创建最终的二进制文件或可执行文件。然后链接器会看到此定义的副本并产生错误。

诊断此问题的一种快速方法是(假设您的构建不需要很长时间):

#warning Piece::Piece(int) is visible here
Piece::Piece(int a) : d_a(a) {
}

应该发出一个警告。如果发出更多,那么编译器可能会提供一些信息(例如包含顺序)。

【讨论】:

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

    【讨论】:

    • #2 解决了!我不小心在“location.h”中包含了“piece.cpp”。谢谢!
    • 哦等等...编译输出中仍然存在重新定义错误,只是 qt 创建者不再在构建问题中显示它。我想我会问一个新问题
    【解决方案3】:

    只是猜测:您是使用#include 还是偶然使用#import - 我曾经遇到过这种情况:-)

    【讨论】:

      【解决方案4】:

      你应该把构造函数的实现放在piece.cpp中,而不是直接放在piece.h中。

      【讨论】:

        【解决方案5】:

        多函数定义错误的最常见原因是将函数定义放在头文件中而忘记将其设置为inline

        我不会太担心char_traits.h - 这可能意味着由于某些模板运行问题,链接器无法找到更好的位置来声明该特定对象文件中发生的定义。

        【讨论】:

          猜你喜欢
          • 2014-03-25
          • 1970-01-01
          • 2013-11-19
          • 1970-01-01
          • 1970-01-01
          • 2018-11-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多