当前位置: 首页 > article >正文

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

我在同一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


http://www.kler.cn/a/403397.html

相关文章:

  • java http body的格式 ‌application/x-www-form-urlencoded‌不支持文件上传
  • 哈希表(极速学习版)
  • ElasticSearch学习篇17_《检索技术核心20讲》最邻近检索-局部敏感哈希、乘积量化PQ思路
  • 基于RTEMS项目学习waf build system
  • 日常开发记录-正确的prop传参,reduce搭配promise的使用
  • 【Unity How】Unity中如何实现物体的匀速往返移动
  • ThinkPHP框架和Laravel框架区别
  • pytorch训练的双卡,一个显卡占有20GB,另一个卡占有8GB,怎么均衡?
  • Elasticsearch面试内容整理-核心概念与数据模型
  • K8S基础概念和环境搭建
  • Flink基础面试题
  • Excel - VLOOKUP函数将指定列替换为字典值
  • 信息与网络安全
  • Java数据库连接(Java Database Connectivity,JDBC)
  • 使用chrome 访问虚拟机Apache2 的默认页面,出现了ERR_ADDRESS_UNREACHABLE这个鸟问题
  • unity3d————范围检测
  • 实现金蝶云与MySQL的无缝数据集成
  • 通过声纹或者声波来切分一段音频
  • nwjs崩溃复现、 nwjs-控制台手动操纵、nwjs崩溃调用栈解码、剪切板例子中、nwjs混合模式、xdotool显示nwjs所有进程窗口列表
  • ubuntu用bind9自建DNS服务器时logging日志出现failed: permission denied解决方法
  • CTFL(六)测试工具
  • QT 自定义界面布局要诀
  • windows C#-异步编程模型(上)
  • MySQL45讲 第二十七讲 主库故障应对:从库切换策略与 GTID 详解——阅读总结
  • strcpy的模拟实现(c基础)
  • XLNet——打破 BERT 局限的预训练语言模型