我正在尝试使用命名空间但是我收到了编译错误:
multiple definition of `B::Class1::Class1()
以下概述了我在这里的代码结构:
//.pro
SOURCES += ../main.cpp \
../a/class1.cpp \
../b/class1.cpp
HEADERS += \
../a/class1.h \
../b/class1.h
// folder a, header
#ifndef CLASS1_H
#define CLASS1_H
namespace A {
class Class1 {
public:
Class1();
};
}
#endif // CLASS1_H
// folder a, source
#include "class1.h"
using namespace A;
Class1::Class1() {
}
// folder b, header
#ifndef CLASS1_H
#define CLASS1_H
namespace B {
class Class1 {
public:
Class1();
void doB();
};
}
#endif // CLASS1_H
// folder b, source
#include "class1.h"
using namespace B;
Class1::Class1() {
}
void B::Class1::doB() {
}
// main.cpp
#include "a/class1.h"
using namespace A;
int main(int argc, char *argv[]) {
Class1 c;
}
我做错了什么?
这是编译输出:
14:39:54: Running steps for project test...
14:39:54: Starting: "C:\Qt\Qt5.3.2\Tools\mingw482_32\bin\mingw32-make.exe" clean
C:/Qt/Qt5.3.2/Tools/mingw482_32/bin/mingw32-make -f Makefile.Debug clean
C:/Qt/Qt5.3.2/Tools/mingw482_32/bin/mingw32-make -f Makefile.Release clean
mingw32-make[1]: Entering directory 'C:/Users/User/Downloads/namespace/build'
Makefile.Debug:133: warning: overriding recipe for target 'debug/class1.o'
Makefile.Debug:130: warning: ignoring old recipe for target 'debug/class1.o'
mingw32-make[1]: Entering directory 'C:/Users/User/Downloads/namespace/build'
Makefile.Release:133: warning: overriding recipe for target 'release/class1.o'
Makefile.Release:130: warning: ignoring old recipe for target 'release/class1.o'
del debug\main.o debug\class1.o debug\class1.o
del release\main.o release\class1.o release\class1.o
Nao foi poss¡vel encontrar C:\Users\User\Downloads\namespace\build\release\main.o
Nao foi poss¡vel encontrar C:\Users\User\Downloads\namespace\build\debug\main.o
mingw32-make[1]: Leaving directory 'C:/Users/User/Downloads/namespace/build'
mingw32-make[1]: Leaving directory 'C:/Users/User/Downloads/namespace/build'
14:39:55: The process "C:\Qt\Qt5.3.2\Tools\mingw482_32\bin\mingw32-make.exe" exited normally.
14:39:55: Configuration unchanged, skipping qmake step.
14:39:55: Starting: "C:\Qt\Qt5.3.2\Tools\mingw482_32\bin\mingw32-make.exe"
C:/Qt/Qt5.3.2/Tools/mingw482_32/bin/mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory 'C:/Users/User/Downloads/namespace/build'
Makefile.Debug:133: warning: overriding recipe for target 'debug/class1.o'
Makefile.Debug:130: warning: ignoring old recipe for target 'debug/class1.o'
g++ -c -pipe -fno-keep-inline-dllexport -std=c++1y -g -frtti -Wall -Wextra -fexceptions -mthreads -DUNICODE -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_NEEDS_QMAIN -I. -I"C:\Qt\Qt5.3.2\5.3\mingw482_32\include" -I"C:\Qt\Qt5.3.2\5.3\mingw482_32\include\QtWidgets" -I"C:\Qt\Qt5.3.2\5.3\mingw482_32\include\QtGui" -I"C:\Qt\Qt5.3.2\5.3\mingw482_32\include\QtCore" -I"debug" -I"C:\Qt\Qt5.3.2\5.3\mingw482_32\mkspecs\win32-g++" -o debug\main.o ..\main.cpp
g++ -c -pipe -fno-keep-inline-dllexport -std=c++1y -g -frtti -Wall -Wextra -fexceptions -mthreads -DUNICODE -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_NEEDS_QMAIN -I. -I"C:\Qt\Qt5.3.2\5.3\mingw482_32\include" -I"C:\Qt\Qt5.3.2\5.3\mingw482_32\include\QtWidgets" -I"C:\Qt\Qt5.3.2\5.3\mingw482_32\include\QtGui" -I"C:\Qt\Qt5.3.2\5.3\mingw482_32\include\QtCore" -I"debug" -I"C:\Qt\Qt5.3.2\5.3\mingw482_32\mkspecs\win32-g++" -o debug\class1.o ..\b\class1.cpp
..\main.cpp:5:5: warning: unused parameter 'argc' [-Wunused-parameter]
int main(int argc, char *argv[]) {
^
..\main.cpp:5:5: warning: unused parameter 'argv' [-Wunused-parameter]
g++ -Wl,-subsystem,windows -mthreads -o debug\test.exe debug/main.o debug/class1.o debug/class1.o -lglu32 -lopengl32 -lgdi32 -luser32 -lmingw32 -LC:/Qt/Qt5.3.2/5.3/mingw482_32/lib -lqtmaind -LC:\mingw482\mingw32\lib -LC:\Utils\icu32_52_1_mingw482\lib -LC:\utils\postgresql\pgsql\lib -LC:\utils\mysql\mysql\lib -LC:\opensll\lib -LC:\Qt\Qt5.3.2\5.3\mingw482_32/lib -lQt5Widgetsd -lQt5Guid -lQt5Cored
debug/class1.o: In function `ZN1B6Class1C2Ev':
C:\Users\User\Downloads\namespace\build/../b/class1.cpp:4: multiple definition of `B::Class1::Class1()'
debug/class1.o:C:\Users\User\Downloads\namespace\build/../b/class1.cpp:4: first defined here
debug/class1.o: In function `ZN1B6Class1C2Ev':
C:\Users\User\Downloads\namespace\build/../b/class1.cpp:4: multiple definition of `B::Class1::Class1()'
debug/class1.o:C:\Users\User\Downloads\namespace\build/../b/class1.cpp:4: first defined here
debug/class1.o: In function `ZN1B6Class13doBEv':
C:\Users\User\Downloads\namespace\build/../b/class1.cpp:7: multiple definition of `B::Class1::doB()'
debug/class1.o:C:\Users\User\Downloads\namespace\build/../b/class1.cpp:7: first defined here
debug/main.o: In function `main':
C:\Users\User\Downloads\namespace\build/../main.cpp:6: undefined reference to `A::Class1::Class1()'
collect2.exe: error: ld returned 1 exit status
Makefile.Debug:81: recipe for target 'debug\test.exe' failed
mingw32-make[1]: Leaving directory 'C:/Users/User/Downloads/namespace/build'
Makefile:34: recipe for target 'debug' failed
mingw32-make[1]: *** [debug\test.exe] Error 1
mingw32-make: *** [debug] Error 2
14:39:55: The process "C:\Qt\Qt5.3.2\Tools\mingw482_32\bin\mingw32-make.exe" exited with code 2.
Error while building/deploying project test (kit: Desktop Qt 5.3 MinGW 32bit)
When executing step "Make"
14:39:55: Elapsed time: 00:01.
答案 0 :(得分:3)
看起来qmake不支持在不同目录中具有相同名称的文件。它最终尝试将/ class1.cpp和b / class1.cpp构建到同一个debug / class1.o文件中,然后将该文件链接两次。
尝试重命名一个* .cpp文件。