多重重定义错误 [英] Multiple redefinition error

查看:556
本文介绍了多重重定义错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在学习了更多关于类和指针的信息之后,我重构了一个程序,并擦除了> 200行代码,在创建另外两个类, Location code> Piece 。问题是,在得到一切编译后,链接器声称, Piece 的构造函数被定义多次,加载错误:



在函数'Piece'中:board.o
'Piece :: Piece(int)'的多重定义`char_traits.h
在函数'Piece':board.o
'Piece :: Piece(int)'的定义`piece.cpp
在函数'Piece'中:player.o
'Piece :: Piece(int)`piece.cpp
在函数'Piece':player.o
'Piece :: Piece(int)`piece.cpp的多重定义(是的,完全相同的错误!)
在函数'Piece':refereee.o
'Piece :: Piece(int)的多重定义`char_traits.h
在函数'Piece':referee.o
多重定义'Piece :: Piece(int)`piece.cpp
...

点击 char_traits.h 的错误,它会给我带来:

  static size_t 
length(const char_type * __s)//错误点到第262行
{return __builtin_strlen(__ s); }

另一个 char_traits.h

  static int 
compare(const char_type * __s1,const char_type * __s2,size_t __n)这里的错误点
{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 ,即使chatter.h和chatter.cpp都不包含任何包含OPiece的内容。



任何想法这个重新定义错误?

解决方案


  1. 重建一切

  2. 对于Piece :: Piece,并删除所有这样的头文件
    2b。从不#include .cpp文件

  3. 查找#define解析为Piece


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?

解决方案

  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

这篇关于多重重定义错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆