2017-03-17 15 views
0

私はシンプルなクラスprogramを作ろうとしていますが、私はそれが問題ではないと思っています。ビルドプロセス中のClionコンパイラエラー

CMakeFiles\Assignment_4.dir/objects.a(Box.cpp.obj):Box.cpp:(.bss+0x0): multiple definition of `Box::objectCount' 
CMakeFiles\Assignment_4.dir/objects.a(Q_1.cpp.obj):Q_1.cpp:(.bss+0x0): first defined here 
collect2.exe: error: ld returned 1 exit status 
mingw32-make.exe[3]: *** [Assignment_4.exe] Error 1 
CMakeFiles\Assignment_4.dir\build.make:147: recipe for target 'Assignment_4.exe' failed 
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/Assignment_4.dir/all' failed 
mingw32-make.exe[2]: *** [CMakeFiles/Assignment_4.dir/all] Error 2 
mingw32-make.exe[1]: *** [CMakeFiles/Assignment_4.dir/rule] Error 2 
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/Assignment_4.dir/rule' failed 
mingw32-make.exe: *** [Assignment_4] Error 2 
Makefile:117: recipe for target 'Assignment_4' failed 

g ++で直接コンパイルしても、複数のオブジェクト定義エラーが発生します。以前はビジュアルスタジオで働いていました。

C:\Users\fsa\CLionProjects\McMaster\Assignment_4>g++ Q_1.cpp clock.cpp box.cpp -o Q1.exe 
C:\Users\fsa\AppData\Local\Temp\ccmD1R6t.o:box.cpp:(.bss+0x0): multiple definition of `Box::objectCount' 
C:\Users\fsa\AppData\Local\Temp\cc0nh7LU.o:Q_1.cpp:(.bss+0x0): first defined here 
collect2.exe: error: ld returned 1 exit status 
+0

間違ったコマンドでコンパイルしていました。これは 'g ++ Q_1.cpp clock.cpp box.cpp -o Q1.exe' –

+0

でなければなりません。 – GigaRohan

+0

@GigaRohanいいえ、しかし、それはエラー出力を変更しました。私のクラスファイルはまったくコンパイルされていませんでした。今は、 'Box :: objectCount''エラーの多重定義があります。私は原因を見るためにコードを実行しています。 –

答えて

0

問題は、int Box::objectCount = 0;がヘッダーにあることです。したがって、それは複数のコンパイル単位(Q1.oとBox.o)に現れます。それはBox.cppに移動する必要があります。

+0

ありがとうございました。しかし、2つのボックスオブジェクトがmain()で作成されていても、カウンタは0にとどまるので、行の配置はカウンタ関数に影響します。 –

+0

私は重要だとは思わないが、通常はCPPファイルのヘッダーの下に配置される。それでも問題が解決しない場合は、デバッガを使って何が起きているのかを確認してください。サイドノート: 'objectCount'はあなたのケースではプライベートでなければなりません。 – GigaRohan

関連する問題