c++ - 命名空间使用导致多重定义

标签 c++ qt c++11 qt5 c++14

我正在尝试使用命名空间,但出现编译错误:

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.

最佳答案

看来qmake不支持不同目录下的同名文件。它最终尝试将 a/class1.cpp 和 b/class1.cpp 构建到同一个 debug/class1.o 文件中,然后将该文件链接两次。

尝试重命名您的一个 *.cpp 文件。

关于c++ - 命名空间使用导致多重定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38879978/

相关文章:

qt - PyQide-Qt QSyntaxHighlighter 多行

c++ - 来自 QWidget 的 QMainWindow 的 setWindowState

c++ - 用户定义的转换运算符优先级,在 g++ 中编译但在 clang++ 中不编译

c++ - decltype 和模板 - 有什么方法可以使 decltype 机器不那么复杂吗?

c++代码无法编译并出现奇怪的错误

c++ - QTGUI - 如何在主窗口上打印来自其他类的消息(QString)?

c++ - 尝试访问 vector 时出现段错误 - cpp

c++ - 使用输入打破 OpenMP 中的循环

C++ line.exe 已停止工作

c++ - 如何在其他线程中运行 io_service?