2017-03-17 136 views
0

我试图做一个简单的类program,但我得到一个奇怪的错误,从我看到的不是代码问题,而是与Clion有关的问题。编译过程中的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 ++直接编译我多个对象定义错误。它在Visual Studio中工作过。

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

谢谢,修复它。但是,行的位置是否会影响计数器函数,因为即使在main()中创建了2个box对象,我的计数器仍然保持为0。 –

+0

我认为不重要,但通常它会放在CPP文件中的标题下。如果你仍然有这个问题,我会说使用调试器来查看你的代码,看看发生了什么。附注:在你的情况下,'objectCount'应该是私人的。 – GigaRohan