Multiple redefinition error

只愿长相守 提交于 2019-12-06 07:40:36
  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

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.

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

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!