多次重定义错误

发布于 2024-09-30 00:11:00 字数 1869 浏览 6 评论 0原文

在了解了有关类和指针的更多信息后,我重构了我的程序并删除了 > 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< 的错误时/code>,它让我想到这一点:

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)。 h 间接来自位置,包括piece.h),board.h是唯一包含location.h的东西,并且一堆类包括board.h

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

有趣的是,其中一个错误有一个“in function 'OPiece':”,之后我的 IDE 放置了 chatter.o,尽管 chatter.h 和 chatter.cpp 都不包含任何内容包括OPiece。

知道什么可能导致此重新定义错误吗?

After learning more about classes and pointers, I refactored a program I had and wiped out > 200 lines of code, in the process creating two other classes, Location and Piece. The problem is, after getting everything to compile, the linker complains that the constructor for Piece is defined multiple times, with a load of errors:

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
...

When I click on the error for char_traits.h, it brings me to this:

static size_t
length(const char_type* __s) //error points here to line 262
{ return __builtin_strlen(__s); }

Another char_traits.h brings me to

  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); }

And just so you know, location.h is the only thing that includes piece.h (well, other files include piece.h indirectly from location including piece.h), board.h is the only thing that includes location.h, and a bunch of classes include board.h

I tried changing the header guard to _OTHELLO_PIECE_H, and tried renaming the class to OPiece (via the IDE). Neither fixed the problem.

The funny thing is, one of the errors has an "in function 'OPiece':", and after that my IDE puts chatter.o, even though neither chatter.h nor chatter.cpp includes anything that includes OPiece.

Any idea what might be causing this redefinition error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

伤感在游骋 2024-10-07 00:11:00
  1. 重建所有内容
  2. 查找 Piece::Piece,并从头文件中删除所有此类内容
    2b.切勿 #include .cpp 文件
  3. 查找解析为 Piece 的 #define
  1. Rebuild everything
  2. Look for Piece::Piece, and remove all such from header files
    2b. Never #include .cpp files
  3. Look for #define that resolves to Piece
花桑 2024-10-07 00:11:00

多函数定义错误的最常见原因是将函数定义放入头文件中并忘记使其内联

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

The most common cause of a multiple function definition error is putting a function definition in a header file and forgetting to make it inline.

I wouldn't worry much about char_traits.h - this probably means that because of some template runaround, the linker couldn't figure out a better place to claim the definition happened in that particular object file.

无戏配角 2024-10-07 00:11:00

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

You should put the implementation of the constructor in piece.cpp, not directly in piece.h.

玉环 2024-10-07 00:11:00

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

1) 接口(在声明处)

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

专用 cpp 文件中

Piece.hpp 中的

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

2) 在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) {
}

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

there are two places to implement Piece::Piece(int) in a typical build:

1) the interface (at declaration)

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

2) in a dedicated cpp file

in Piece.hpp

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

in Piece.cpp

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

however, templates are defined differently.

the error often indicates that Piece::Piece(int a) : d_a(a) {} is included in multiple cpp files.

each object file produced adds the symbol Piece::Piece(int) where it is visible.

at the end of the build, all objects are merged/linked to create your final binary or executable. then the linker sees that there are copies of this definition and produces an error.

one quick way to diagnose this is (assuming your builds do not take long):

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

exactly one warning should be emitted. if more are emitted, then the compiler will likely provide a bit of information (such as the include order).

梦毁影碎の 2024-10-07 00:11:00

只是猜测:你是使用了#include,还是偶然使用了#import——这发生在我身上一次:-)

Just a guess: Did you use #include or did you use #import by accident -- which happened to me once :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文