2017-10-17 13 views
0

私は3つのファイルを持つ簡単なプロジェクトを持っています。 Main.cpp、CountTriangles.cpp、およびCountTriangles.hpp。ビルド/実行しようとすると、「リンカコマンドが終了コード1で失敗しました」というメッセージが表示され、ログに「x86_64アーキテクチャのld:1重複シンボル」が見つかりました。リンカコマンドが失敗しました。重複シンボル

main.cppに:

#include "CountTriangles.cpp" 

int main(int argc, const char * argv[]) { 
    return 0; 
} 

CountTriangles.cpp:

#include "CountTriangles.hpp" 
using namespace std; 

int TriangleCount::count(int N){ 
    int helper = 1; 
    return helper; 
} 

CountTriangles.hpp:main.cpp

#ifndef CountTriangles_hpp 
#define CountTriangles_hpp 

#include <iostream> 
#include <stdio.h> 

class TriangleCount{ 
    public: 
     int count(int N); 
}; 

#endif /* CountTriangles_hpp */ 
+1

ヘッダーファイルではなく、*ソースファイルをインクルードします。それをしないでください。 –

+0

メインでは、.hppファイルの代わりに '#include" CountTriangles.cpp "を含めます。 –

+2

今後の質問については、良い質問をする方法をお読みください](http://stackoverflow.com/help/how-to-ask)。エラー出力を実際に*コピー・ペーストする必要があります。存在する可能性のあるすべての情報メモを完全に、完全に、 –

答えて

1

あなたが#include "CountTriangles.cpp"が含まれていますが、ヘッダーを含めにする必要がありますCountTriangles.hpp

TriangleCount::count(int N)の定義が2回コンパイルされているため、再定義されると、結果として重複するシンボルエラーが発生します。

関連する問題