2010-12-04 10 views
1

私はゲームを複数のファイルに分割しようとしました。これらのエラーが表示されます。C++リンカエラー

1>item.obj : error LNK2005: "private: static class sf::Image ctile::itile" ([email protected]@@[email protected]@@A) already defined in character.obj 
1>item.obj : error LNK2005: "class cmap maps" ([email protected]@[email protected]@A) already defined in character.obj 
1>item.obj : error LNK2005: "class cmainchar mainch" ([email protected]@[email protected]@A) already defined in character.obj 
1>item.obj : error LNK2005: "class citemmanager itemmanager" ([email protected]@[email protected]@A) already defined in character.obj 
1>item.obj : error LNK2005: "private: static class sf::Image citem::iitem" ([email protected]@@[email protected]@@A) already defined in character.obj 
1>item.obj : error LNK2005: "private: static class sf::Image cspell::ispell" ([email protected]@@[email protected]@@A) already defined in character.obj 
1>item.obj : error LNK2005: "class sf::RenderWindow App" ([email protected]@[email protected]@@A) already defined in character.obj 
1>item.obj : error LNK2005: "class sf::View View" ([email protected]@[email protected]@A) already defined in character.obj 
1>main.obj : error LNK2005: "private: static class sf::Image ctile::itile" ([email protected]@@[email protected]@@A) already defined in character.obj 
1>main.obj : error LNK2005: "class cmap maps" ([email protected]@[email protected]@A) already defined in character.obj 
1>main.obj : error LNK2005: "class cmainchar mainch" ([email protected]@[email protected]@A) already defined in character.obj 
1>main.obj : error LNK2005: "class citemmanager itemmanager" ([email protected]@[email protected]@A) already defined in character.obj 
1>main.obj : error LNK2005: "private: static class sf::Image citem::iitem" ([email protected]@@[email protected]@@A) already defined in character.obj 
1>main.obj : error LNK2005: "private: static class sf::Image cspell::ispell" ([email protected]@@[email protected]@@A) already defined in character.obj 
1>main.obj : error LNK2005: "class sf::RenderWindow App" ([email protected]@[email protected]@@A) already defined in character.obj 
1>main.obj : error LNK2005: "class sf::View View" ([email protected]@[email protected]@A) already defined in character.obj 
1>map.obj : error LNK2005: "private: static class sf::Image ctile::itile" ([email protected]@@[email protected]@@A) already defined in character.obj 
1>map.obj : error LNK2005: "class cmap maps" ([email protected]@[email protected]@A) already defined in character.obj 
1>map.obj : error LNK2005: "class cmainchar mainch" ([email protected]@[email protected]@A) already defined in character.obj 
1>map.obj : error LNK2005: "class citemmanager itemmanager" ([email protected]@[email protected]@A) already defined in character.obj 
1>map.obj : error LNK2005: "private: static class sf::Image citem::iitem" ([email protected]@@[email protected]@@A) already defined in character.obj 
1>map.obj : error LNK2005: "private: static class sf::Image cspell::ispell" ([email protected]@@[email protected]@@A) already defined in character.obj 
1>map.obj : error LNK2005: "class sf::RenderWindow App" ([email protected]@[email protected]@@A) already defined in character.obj 
1>map.obj : error LNK2005: "class sf::View View" ([email protected]@[email protected]@A) already defined in character.obj 
1>spell.obj : error LNK2005: "private: static class sf::Image ctile::itile" ([email protected]@@[email protected]@@A) already defined in character.obj 
1>spell.obj : error LNK2005: "class cmap maps" ([email protected]@[email protected]@A) already defined in character.obj 
1>spell.obj : error LNK2005: "class cmainchar mainch" ([email protected]@[email protected]@A) already defined in character.obj 
1>spell.obj : error LNK2005: "class citemmanager itemmanager" ([email protected]@[email protected]@A) already defined in character.obj 
1>spell.obj : error LNK2005: "private: static class sf::Image citem::iitem" ([email protected]@@[email protected]@@A) already defined in character.obj 
1>spell.obj : error LNK2005: "private: static class sf::Image cspell::ispell" ([email protected]@@[email protected]@@A) already defined in character.obj 
1>spell.obj : error LNK2005: "class sf::RenderWindow App" ([email protected]@[email protected]@@A) already defined in character.obj 
1>spell.obj : error LNK2005: "class sf::View View" ([email protected]@[email protected]@A) already defined in character.obj 
+5

*あなたのゲーム*で始めるのではなく、単純な "hello world"スタイルのアプリケーションを2つ以上のファイルに分割することをお勧めします。そうすれば、(a)ここにコードを掲載し、(b)重要な概念をより簡単に理解することができます。エラーメッセージの一覧だけでコードをデバッグすることはできません。 –

答えて

2

問題は、クラスメンバー関数の定義がヘッダーファイルに配置されていることです。したがって、同じ定義は別々の翻訳単位に行きます。

また、ヘッダーファイルにinclude guardsを使用してください。

+1

私は本当にあなたの答えをdownvoteしたい。たぶん彼はガードを含んでいないかもしれませんが、エラーはリンカーから来ているので、ガードはそれを解決しません。 – ybungalobill

+0

@ybungalobill:LNK2005は多重定義エラーです。したがって、OPが、同じ変換単位内に複数のインクルードガードを含まないヘッダファイルを含む場合、リンカはこのエラーをスローします。インクルードガードを追加するとこの問題が解決されます。 – Praetorian

+0

@ybungalobill完全にあなたに同意します。私は答えを修正した。それは私が眠る必要があるようだ:-) – Stas

4

おそらく、メソッド定義は.hファイルになります。 .cppファイルで

#ifndef SOMETHING_H_ 
#define SOMETHING_H_ 

class Something { 
public: 
    int foo(); 
}; 

#endif // SOMETHING_H_ 

とメソッドの定義::

その結果、あなたは警備員を含めると.hファイルで宣言をしてください、あなたの

における単一の定義の多くのコピーを持っています

#include "Something.h" 

int Something::foo() { 
    return 5; 
} 
+0

私にはヘッダーガードがあり、ヘッダーにはメソッドがありません。 – Yelnats

+1

あなたは何か間違っている。最初のエラーを見てください: '1> item.obj:エラーLNK2005:character.objで既に定義されている" private:静的クラスsf :: Image ctile :: itile "(?itile @ ctile @@ 0VImage @ sf @@ A) '基本的には、このクラスは' item.obj'(item.cppをコンパイルした結果、私はpressume)と 'character.obj'の両方で定義されています。これは、通常、インクルードガードを省略することによって "達成"され、私はあなたがそれ以外の事故でそれをどうやってやったのか分かりません。これらのファイルを投稿できますか(そのクラスの2倍の.cpp +ヘッダー)? – Kos

+0

私は、問題がヘッダのインスタンスを宣言していたことがわかりました。 – Yelnats