クラスとポインターについてさらに学習した後、私は持っていたプログラムをリファクタリングし、他の 2 つのクラスを作成する過程で 200 行を超えるコードを削除しましLocation
たPiece
。問題は、すべてをコンパイルした後、リンカーがコンストラクター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 を含む場所から間接的に piece.h がインクルードされます)、board.h は location.h をインクルードする唯一のものです。クラスの束にはboard.hが含まれます
ヘッダー ガードを_OTHELLO_PIECE_H
に変更し、クラスの名前を (IDE 経由で) OPiece に変更しようとしました。どちらも問題を解決しませんでした。
面白いことに、エラーの 1 つに「in function 'OPiece':」があり、その後、chatter.o
chatter.h にも chatter.cpp にも OPiece を含むものは何も含まれていないにもかかわらず、私の IDE は を置きます。
この再定義エラーの原因は何ですか?