Qt问题:不同文件中相同命名空间的多个 Q_NAMESPACE

时间:2024-11-17 07:03:32

我在同一namespace中定义了2个enum,定义如下:

a.h

//
// Created by qiaowei on 2024/11/15.
//

#ifndef ELECTRICITY_MONTHS_ENUM_H
#define ELECTRICITY_MONTHS_ENUM_H


#include <QMetaObject>


namespace data {
	
	Q_NAMESPACE
	
	enum class Months_enum {
		
		january,
		
		february,
		
		march,
		
		april,
		
		may,
		
		june,
		
		july,
		
		august,
		
		september,
		
		october,
		
		november,
		
		december
	};
	
	Q_ENUM_NS(Months_enum)
	
} // data

#endif //ELECTRICITY_MONTHS_ENUM_H

b.h

//
// Created by qiaowei on 2024/11/8.
//

#ifndef ELECTRICITY_EXPENSE_ENUM_H
#define ELECTRICITY_EXPENSE_ENUM_H


#include <QMetaObject>


namespace data {
	
    Q_NAMESPACE

    enum class Expense_enum {

        electricity_bill,

        oil_bill,

        water_bill,

        gas_bill,

        internet_bill,

        other_bill
    };

    Q_ENUM_NS(Expense_enum)
	
} // data

#endif //ELECTRICITY_EXPENSE_ENUM_H

编译时报“ error C2011: “qt_meta_stringdata_data_t”:“struct”类型重定义”类似错误:

====================[ Build | Electricity | Debug ]=============================
C:\ProgramFiles\cmake-3.30.2-windows-x86_64\bin\cmake.exe --build D:\programming\source-code\cpp\Electricity\cmake-build-debug --target Electricity -j 14
[1/4] Automatic MOC and UIC for target Electricity
[2/4] Building CXX object CMakeFiles\Electricity.dir\data\enum\months_enum.cpp.obj
[3/4] Building CXX object CMakeFiles\Electricity.dir\Electricity_autogen\mocs_compilation.cpp.obj
FAILED: CMakeFiles/Electricity.dir/Electricity_autogen/mocs_compilation.cpp.obj 
C:\PROGRA~4\MICROS~1\2022\BUILDT~1\VC\Tools\MSVC\1437~1.328\bin\Hostx64\x64\cl.exe  /nologo /TP -DQT_CORE_LIB -DQT_GUI_LIB -DQT_UITOOLS_LIB -DQT_WIDGETS_LIB -ID:\programming\source-code\cpp\Electricity\.\include\qxlsx -ID:\programming\source-code\cpp\Electricity\.\include\test -external:ID:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\include -external:IC:\ProgramFiles\Qt\5.15.2\msvc2019_64\include -external:IC:\ProgramFiles\Qt\5.15.2\msvc2019_64\include\QtCore -external:IC:\ProgramFiles\Qt\5.15.2\msvc2019_64\.\mkspecs\win32-msvc -external:IC:\ProgramFiles\Qt\5.15.2\msvc2019_64\include\QtGui -external:IC:\ProgramFiles\Qt\5.15.2\msvc2019_64\include\QtANGLE -external:IC:\ProgramFiles\Qt\5.15.2\msvc2019_64\include\QtWidgets -external:IC:\ProgramFiles\Qt\5.15.2\msvc2019_64\include\QtUiTools -external:W0 /DWIN32 /D_WINDOWS /EHsc /Ob0 /Od /RTC1 -std:c++17 -MDd -Zi /utf-8 /showIncludes /FoCMakeFiles\Electricity.dir\Electricity_autogen\mocs_compilation.cpp.obj /FdCMakeFiles\Electricity.dir\ /FS -c D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\mocs_compilation.cpp
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(24): error C2011: “qt_meta_stringdata_data_t”:“struct”类型重定义
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_expense_enum.cpp(24): note: 参见“qt_meta_stringdata_data_t”的声明
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(33): error C2374: “qt_meta_stringdata_data”: 重定义;多次初始化
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_expense_enum.cpp(33): note: 参见“qt_meta_stringdata_data”的声明
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(35): error C2027: 使用了未定义类型“qt_meta_stringdata_data_t”
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_expense_enum.cpp(24): note: 参见“qt_meta_stringdata_data_t”的声明
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(35): error C2618: offsetof 中的非法成员指示符
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(35): note: offsetof 具有内在含义;使用 /Zc:offsetof- 还原到旧的非符合定义
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(36): error C2027: 使用了未定义类型“qt_meta_stringdata_data_t”
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_expense_enum.cpp(24): note: 参见“qt_meta_stringdata_data_t”的声明
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(36): error C2618: offsetof 中的非法成员指示符
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(36): note: offsetof 具有内在含义;使用 /Zc:offsetof- 还原到旧的非符合定义
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(37): error C2027: 使用了未定义类型“qt_meta_stringdata_data_t”
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_expense_enum.cpp(24): note: 参见“qt_meta_stringdata_data_t”的声明
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(37): error C2618: offsetof 中的非法成员指示符
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(37): note: offsetof 具有内在含义;使用 /Zc:offsetof- 还原到旧的非符合定义
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(38): error C2027: 使用了未定义类型“qt_meta_stringdata_data_t”
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_expense_enum.cpp(24): note: 参见“qt_meta_stringdata_data_t”的声明
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(38): error C2618: offsetof 中的非法成员指示符
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(38): note: offsetof 具有内在含义;使用 /Zc:offsetof- 还原到旧的非符合定义
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(39): error C2027: 使用了未定义类型“qt_meta_stringdata_data_t”
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_expense_enum.cpp(24): note: 参见“qt_meta_stringdata_data_t”的声明
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(39): error C2618: offsetof 中的非法成员指示符
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(39): note: offsetof 具有内在含义;使用 /Zc:offsetof- 还原到旧的非符合定义
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(40): error C2027: 使用了未定义类型“qt_meta_stringdata_data_t”
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_expense_enum.cpp(24): note: 参见“qt_meta_stringdata_data_t”的声明
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(40): error C2618: offsetof 中的非法成员指示符
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(40): note: offsetof 具有内在含义;使用 /Zc:offsetof- 还原到旧的非符合定义
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(41): error C2027: 使用了未定义类型“qt_meta_stringdata_data_t”
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_expense_enum.cpp(24): note: 参见“qt_meta_stringdata_data_t”的声明
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(41): error C2618: offsetof 中的非法成员指示符
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(41): note: offsetof 具有内在含义;使用 /Zc:offsetof- 还原到旧的非符合定义
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(42): error C2027: 使用了未定义类型“qt_meta_stringdata_data_t”
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_expense_enum.cpp(24): note: 参见“qt_meta_stringdata_data_t”的声明
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(42): error C2618: offsetof 中的非法成员指示符
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(42): note: offsetof 具有内在含义;使用 /Zc:offsetof- 还原到旧的非符合定义
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(43): error C2027: 使用了未定义类型“qt_meta_stringdata_data_t”
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_expense_enum.cpp(24): note: 参见“qt_meta_stringdata_data_t”的声明
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(43): error C2618: offsetof 中的非法成员指示符
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(43): note: offsetof 具有内在含义;使用 /Zc:offsetof- 还原到旧的非符合定义
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(44): error C2027: 使用了未定义类型“qt_meta_stringdata_data_t”
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_expense_enum.cpp(24): note: 参见“qt_meta_stringdata_data_t”的声明
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(44): error C2618: offsetof 中的非法成员指示符
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(44): note: offsetof 具有内在含义;使用 /Zc:offsetof- 还原到旧的非符合定义
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(45): error C2027: 使用了未定义类型“qt_meta_stringdata_data_t”
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_expense_enum.cpp(24): note: 参见“qt_meta_stringdata_data_t”的声明
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(45): error C2618: offsetof 中的非法成员指示符
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(45): note: offsetof 具有内在含义;使用 /Zc:offsetof- 还原到旧的非符合定义
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(46): error C2027: 使用了未定义类型“qt_meta_stringdata_data_t”
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_expense_enum.cpp(24): note: 参见“qt_meta_stringdata_data_t”的声明
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(46): error C2618: offsetof 中的非法成员指示符
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(46): note: offsetof 具有内在含义;使用 /Zc:offsetof- 还原到旧的非符合定义
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(47): error C2027: 使用了未定义类型“qt_meta_stringdata_data_t”
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_expense_enum.cpp(24): note: 参见“qt_meta_stringdata_data_t”的声明
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(47): error C2618: offsetof 中的非法成员指示符
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(47): note: offsetof 具有内在含义;使用 /Zc:offsetof- 还原到旧的非符合定义
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(48): error C2027: 使用了未定义类型“qt_meta_stringdata_data_t”
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_expense_enum.cpp(24): note: 参见“qt_meta_stringdata_data_t”的声明
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(48): error C2618: offsetof 中的非法成员指示符
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(48): note: offsetof 具有内在含义;使用 /Zc:offsetof- 还原到旧的非符合定义
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(33): error C2027: 使用了未定义类型“qt_meta_stringdata_data_t”
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_expense_enum.cpp(24): note: 参见“qt_meta_stringdata_data_t”的声明
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(57): error C2374: “qt_meta_data_data”: 重定义;多次初始化
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_expense_enum.cpp(51): note: 参见“qt_meta_data_data”的声明
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(90): error C2370: “data::staticMetaObject”: 重定义;不同的存储类
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4\../../../data/enum/months_enum.h(14): note: 参见“data::staticMetaObject”的声明
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(92): error C2027: 使用了未定义类型“qt_meta_stringdata_data_t”
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_expense_enum.cpp(24): note: 参见“qt_meta_stringdata_data_t”的声明
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(93): error C2440: “初始化”: 无法从“const uint [32]”转换为“const QByteArrayData *”
D:\programming\source-code\cpp\Electricity\cmake-build-debug\Electricity_autogen\PM3IPG3PG4/moc_months_enum.cpp(93): note: 指向的类型不相关; 转换需要 reinterpret_cast、C 样式强制转换或带圆括号的函数样式强制转换
ninja: build stopped: subcommand failed.

高赞回答这是Qt的moc机制原因,目前无法解决:

官方 Qt 问题跟踪器中存在相应的问题:

QTBUG-68611。然而,该问题因超出范围而被关闭,因为由于 Qt 元对象编译器 (moc) 的设计,对此无能为力。

简而言之,Qt 不支持多个头文件,每个头文件都有

Q_NAMESPACE

 宏。这是不可能的。

或者,您可以

(尽管我不建议)有一个中间文件结构,如下所示:

// internal/C1.h #include <QObject> enum class Enum1 {A, B}; Q_ENUM_NS(Enum1)
 
 //internal/C2.h #include <QObject> enum class Enum2 {A, B}; Q_ENUM_NS(Enum1) 
 
 
 

// C.h #include <QObject> namespace SW { Q_NAMESPACE #include internal/C1.h #include internal/C2.h }

 参考:https://www.soinside.com/question/Rk6AQ7khQe7AmtjRNdpnJF