我试图利用命名空间,但我得到一个编译错误:
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:QtQt5.3.2Toolsmingw482_32binmingw32-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 debugmain.o debugclass1.o debugclass1.o
del releasemain.o releaseclass1.o releaseclass1.o
Nao foi poss¡vel encontrar C:UsersUserDownloadsnamespacebuildreleasemain.o
Nao foi poss¡vel encontrar C:UsersUserDownloadsnamespacebuilddebugmain.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:QtQt5.3.2Toolsmingw482_32binmingw32-make.exe" exited normally.
14:39:55: Configuration unchanged, skipping qmake step.
14:39:55: Starting: "C:QtQt5.3.2Toolsmingw482_32binmingw32-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:QtQt5.3.25.3mingw482_32include" -I"C:QtQt5.3.25.3mingw482_32includeQtWidgets" -I"C:QtQt5.3.25.3mingw482_32includeQtGui" -I"C:QtQt5.3.25.3mingw482_32includeQtCore" -I"debug" -I"C:QtQt5.3.25.3mingw482_32mkspecswin32-g++" -o debugmain.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:QtQt5.3.25.3mingw482_32include" -I"C:QtQt5.3.25.3mingw482_32includeQtWidgets" -I"C:QtQt5.3.25.3mingw482_32includeQtGui" -I"C:QtQt5.3.25.3mingw482_32includeQtCore" -I"debug" -I"C:QtQt5.3.25.3mingw482_32mkspecswin32-g++" -o debugclass1.o ..bclass1.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 debugtest.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:mingw482mingw32lib -LC:Utilsicu32_52_1_mingw482lib -LC:utilspostgresqlpgsqllib -LC:utilsmysqlmysqllib -LC:openslllib -LC:QtQt5.3.25.3mingw482_32/lib -lQt5Widgetsd -lQt5Guid -lQt5Cored
debug/class1.o: In function `ZN1B6Class1C2Ev':
C:UsersUserDownloadsnamespacebuild/../b/class1.cpp:4: multiple definition of `B::Class1::Class1()'
debug/class1.o:C:UsersUserDownloadsnamespacebuild/../b/class1.cpp:4: first defined here
debug/class1.o: In function `ZN1B6Class1C2Ev':
C:UsersUserDownloadsnamespacebuild/../b/class1.cpp:4: multiple definition of `B::Class1::Class1()'
debug/class1.o:C:UsersUserDownloadsnamespacebuild/../b/class1.cpp:4: first defined here
debug/class1.o: In function `ZN1B6Class13doBEv':
C:UsersUserDownloadsnamespacebuild/../b/class1.cpp:7: multiple definition of `B::Class1::doB()'
debug/class1.o:C:UsersUserDownloadsnamespacebuild/../b/class1.cpp:7: first defined here
debug/main.o: In function `main':
C:UsersUserDownloadsnamespacebuild/../main.cpp:6: undefined reference to `A::Class1::Class1()'
collect2.exe: error: ld returned 1 exit status
Makefile.Debug:81: recipe for target 'debugtest.exe' failed
mingw32-make[1]: Leaving directory 'C:/Users/User/Downloads/namespace/build'
Makefile:34: recipe for target 'debug' failed
mingw32-make[1]: *** [debugtest.exe] Error 1
mingw32-make: *** [debug] Error 2
14:39:55: The process "C:QtQt5.3.2Toolsmingw482_32binmingw32-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.
看起来qmake不支持不同目录下具有相同名称的文件。它最终尝试将a/class1.cpp和b/class1.cpp构建到同一个debug/class1中。O文件,然后链接该文件两次。
尝试重命名一个*.cpp文件