C++ enum class实现& | ^ ~等操作

enum class FutureState {
    ASYNC = 0x01,
    DEFERRED = 0x02,
};

inline FutureState operator&(FutureState el, FutureState er)
{
    return static_cast<FutureState>(static_cast<int>(el) & static_cast<int>(er));
}

inline FutureState operator|(FutureState el, FutureState er)
{
    return static_cast<FutureState>(static_cast<int>(el) | static_cast<int>(er));
}

inline FutureState operator^(FutureState el, FutureState er)
{
    return static_cast<FutureState>(static_cast<int>(el) ^ static_cast<int>(er));
}

inline FutureState operator~(FutureState el)
{
    return static_cast<FutureState>(~static_cast<int>(el));
}

inline FutureState& operator&=(FutureState& el, FutureState er)
{
    return el = el & er;
}

inline FutureState& operator|=(FutureState& el, FutureState er)
{
    return el = el | er;
}

inline FutureState& operator^=(FutureState& el, FutureState er)
{
    return el = el ^ er;
}

int main(int argc, char **argv)
{
    FutureState state1 = FutureState::ASYNC & FutureState::DEFERRED;
    FutureState state2 = FutureState::ASYNC | FutureState::DEFERRED;
    FutureState state3 = ~state1;
    FutureState state4 = state1 ^ state2;

    state3 &= state1;
    state4 |= state2;
    state4 ^= state2;
    return 0;
}
In file included from include/mbgl/util/image.hpp:4, from platform/qt/src/qt_image.cpp:1: include/mbgl/util/geometry.hpp:9:24: error: found &lsquo;:&rsquo; in nested-name-specifier, expected &lsquo;::&rsquo; 9 | enum class FeatureType : uint8_t { | ^ | :: include/mbgl/util/geometry.hpp:9:12: error: &lsquo;FeatureType&rsquo; has not been declared 9 | enum class FeatureType : uint8_t { | ^~~~~~~~~~~ include/mbgl/util/geometry.hpp:9:34: error: expected unqualified-id before &lsquo;{&rsquo; token 9 | enum class FeatureType : uint8_t { | ^ include/mbgl/util/geometry.hpp:47:5: error: &lsquo;FeatureType&rsquo; does not name a type; did you mean &lsquo;ToFeatureType&rsquo;? 47 | FeatureType operator()(const Point&lt;T&gt; &amp;) const { return FeatureType::Point; } | ^~~~~~~~~~~ | ToFeatureType include/mbgl/util/geometry.hpp:49:5: error: &lsquo;FeatureType&rsquo; does not name a type; did you mean &lsquo;ToFeatureType&rsquo;? 49 | FeatureType operator()(const MultiPoint&lt;T&gt; &amp;) const { return FeatureType::Point; } | ^~~~~~~~~~~ | ToFeatureType include/mbgl/util/geometry.hpp:51:5: error: &lsquo;FeatureType&rsquo; does not name a type; did you mean &lsquo;ToFeatureType&rsquo;? 51 | FeatureType operator()(const LineString&lt;T&gt; &amp;) const { return FeatureType::LineString; } | ^~~~~~~~~~~ | ToFeatureType include/mbgl/util/geometry.hpp:53:5: error: &lsquo;FeatureType&rsquo; does not name a type; did you mean &lsquo;ToFeatureType&rsquo;? 53 | FeatureType operator()(const MultiLineString&lt;T&gt; &amp;) const { return FeatureType::LineString; } | ^~~~~~~~~~~ | ToFeatureType include/mbgl/util/geometry.hpp:55:5: error: &lsquo;FeatureType&rsquo; does not name a type; did you mean &lsquo;ToFeatureType&rsquo;? 55 | FeatureType operator()(const Polygon&lt;T&gt; &amp;) const { return FeatureType::Polygon; } | ^~~~~~~~~~~ | ToFeatureType include/mbgl/util/geometry.hpp:57:5: error: &lsquo;FeatureType&rsquo; does not name a type; did you mean &lsquo;ToFeatureType&rsquo;? 57 | FeatureType operator()(const MultiPolygon&lt;T&gt; &amp;) const { return FeatureType::Polygon; } | ^~~~~~~~~~~ | ToFeatureType include/mbgl/util/geometry.hpp:59:5: error: &lsquo;FeatureType&rsquo; does not name a type; did you mean &lsquo;ToFeatureType&rsquo;? 59 | FeatureType operator()(const mapbox::geometry::geometry_collection&lt;T&gt; &amp;) const { return FeatureType::Unknown; } | ^~~~~~~~~~~ | ToFeatureType make[5]: *** [Makefile:6161:.obj/platform/qt/src/qt_image.o] 错误 1 make[5]: *** 正在等待未完成的任务.... make[5]: 离开目录&ldquo;/opt/qt-everywhere-src-5.15.17/qtlocation/src/3rdparty/mapbox-gl-native&rdquo; make[4]: *** [Makefile:203:sub-------3rdparty-mapbox-gl-native-make_first] 错误 2 make[4]: 离开目录&ldquo;/opt/qt-everywhere-src-5.15.17/qtlocation/src/plugins/geoservices&rdquo; make[3]: *** [Makefile:73:sub-geoservices-make_first] 错误 2 make[3]: 离开目录&ldquo;/opt/qt-everywhere-src-5.15.17/qtlocation/src/plugins&rdquo; make[2]: *** [Makefile:234:sub-plugins-make_first] 错误 2 make[2]: 离开目录&ldquo;/opt/qt-everywhere-src-5.15.17/qtlocation/src&rdquo; make[1]: *** [Makefile:49:sub-src-make_first] 错误 2 make[1]: 离开目录&ldquo;/opt/qt-everywhere-src-5.15.17/qtlocation&rdquo; make: *** [Makefile:813:module-qtlocation] 错误 2
06-18
&lt;think&gt;我们正在处理Qt5.15.17编译过程中出现的错误,具体是qtlocation模块的geoservices子模块构建失败。根据用户提供的错误信息,我们注意到一个关键点:`error:&#39;uint8_t&#39;doesnotnameatype`。这表明编译器无法识别`uint8_t`类型。###问题分析在C++中,`uint8_t`等固定宽度整数类型定义在`&lt;cstdint&gt;`头文件(C++风格)或`&lt;stdint.h&gt;`(C风格)中。错误提示表明在代码中使用了`uint8_t`但未包含相应的头文件。在Qt5.15.17的源码中,如果某个文件使用了`uint8_t`而没有包含`&lt;cstdint&gt;`或`&lt;stdint.h&gt;`,就会导致此编译错误。###解决方案####步骤1:定位问题文件我们需要找到在`qtlocation/src/plugins/geoservices/osm`目录下哪个源文件使用了`uint8_t`类型。可以通过以下命令搜索:```bashcd/opt/qt-everywhere-src-5.15.17/qtlocation/src/plugins/geoservices/osmgrep-rn&quot;uint8_t&quot;.```####步骤2:添加头文件在找到的源文件中,添加包含`&lt;cstdint&gt;`的头文件。例如,如果问题出现在`qgeotiledmappingmanagerengineosm.cpp`中,则在文件开头添加:```cpp#include&lt;cstdint&gt;```####步骤3:重新编译修改后,返回到`geoservices`目录,重新编译:```bashcd..make-j$(nproc)```####步骤4:如果问题仍然存在如果错误仍然出现,可能是因为在头文件中使用了`uint8_t`而没有包含相应的头文件。同样,检查头文件并添加`#include&lt;cstdint&gt;`。###注意事项-在Qt5.15中,通常应该使用`&lt;cstdint&gt;`(C++11标准头文件)。确保你的编译器支持C++11。-如果代码中使用了`uint32_t`等类型,同样需要包含该头文件。###完整示例假设我们在`qgeotiledmappingmanagerengineosm.cpp`中发现了使用`uint8_t`的代码行,我们在该文件的开头添加:```cpp//...其他头文件#include&lt;cstdint&gt;//添加这一行```###重新编译整个Qt在修复了该文件后,可以尝试重新编译整个qtlocation模块:```bashcd/opt/qt-everywhere-src-5.15.17makemodule-qtlocation-j$(nproc)```###备选方案:全局定义如果问题出现在多个文件中,且修改每个文件很麻烦,可以考虑在项目的预编译头文件中添加包含(如果有使用预编译头文件)。但在Qt的模块中,通常不建议这样做,因为每个模块是独立的。###为什么会出现这个问题?可能的原因:1.该文件原本在包含其他头文件时间接包含了`&lt;cstdint&gt;`,但由于头文件包含顺序的变化,导致编译时未能包含。2.在升级编译器或更改编译选项后,原本隐式包含的头文件不再被包含。###验证编译器标志确保编译时启用了C++11或更高版本。在Qt5.15中,默认应该已经启用了C++11,但可以检查`Makefile`中是否有`-std=c++11`或类似的标志。如果没有,可以在项目的配置中添加。在`geoservices.pro`文件中,可以尝试添加:```qmakeCONFIG+=c++11```但是,在Qt5.15中,通常不需要手动添加,因为Qt本身已经要求C++11。如果问题仍然存在,可以尝试在`geoservices.pro`中添加:```qmakeQMAKE_CXXFLAGS+=-std=c++11```###总结1.找到使用`uint8_t`的源文件或头文件。2.在文件中添加`#include&lt;cstdint&gt;`。3.重新编译。通过以上步骤,应该能够解决`uint8_t`未定义的问题,从而成功编译qtlocation模块。&sect;&sect;相关问题&sect;&sect;1.如何为Qt模块添加全局的编译选项?2.Qt5中C++11的支持情况是怎样的?3.如何正确包含C++标准库头文件?&lt;/think&gt;### 解决Qt 5.15.17编译错误:`enum class FeatureType : uint8_t` 导致make失败 #### 问题分析 您遇到的`&#39;uint8_t&#39; does not name a type`错误是由于C++编译器无法识别`uint8_t`类型导致的。这在Qt 5.15.17编译中常见于使用C++11特性的代码(如强类型枚举)但缺少必要的头文件包含[^1]。 #### 解决方案 ##### 步骤1:添加缺失的头文件 在报错源文件中添加`#include &lt;cstdint&gt;`: ```cpp // 在包含FeatureType枚举定义的文件顶部添加 #include &lt;cstdint&gt; // 解决uint8_t未定义问题 enum class FeatureType : uint8_t { Point, LineString, Polygon }; ``` ##### 步骤2:检查编译器C++标准支持 确保编译器支持C++11标准: ```bash # 检查gcc支持的C++标准 g++ -dM -E -x c++ /dev/null | grep __cplusplus # 输出应为:__cplusplus 201103L 或更高 ``` ##### 步骤3:在Qt配置中显式启用C++11 在configure命令中添加C++11标志: ```bash ./configure \ -platform linux-g++ \ -c++std c++11 \ # 显式启用C++11支持 ...其他配置参数... ``` ##### 步骤4:修复具体错误位置 根据堆栈跟踪定位到具体文件: ```bash cd /opt/qt-everywhere-src-5.15.17/qtlocation/src/plugins/geoservices/osm grep -rn &quot;FeatureType&quot; . # 查找使用FeatureType的文件 ``` 在找到的文件中(通常是`qgeotiledmappingmanagerengineosm.cpp`或类似文件): 1. 添加`#include &lt;cstdint&gt;` 2. 检查所有使用`uint8_t`的地方 ##### 步骤5:清理并重新编译 ```bash # 清理之前的编译 cd /opt/qt-everywhere-src-5.15.17 make distclean # 重新生成Makefile ./configure -c++std c++11 ...其他参数... # 仅编译qtlocation模块 make module-qtlocation -j$(nproc) ``` #### 技术原理 1. `uint8_t`是C++11在`&lt;cstdint&gt;`中定义的精确宽度整数类型 2. Qt 5.15默认要求C++11支持,但某些系统环境可能: - 编译器默认使用旧标准(如C++98) - 缺少必要的头文件包含 3. 强类型枚举`enum class : uint8_t`语法依赖C++11特性 #### 验证修复 成功编译后,检查生成的库文件: ```bash nm -C /path/to/libqtgeoservices_osm.so | grep FeatureType ``` 应显示正确的符号名称(如`FeatureType::Point`)而非错误信息[^2]。 #### 备选方案 如果问题持续,尝试在`.pro`文件中添加: ```qmake # 在geoservices.pro中添加 CONFIG += c++11 QMAKE_CXXFLAGS += -include cstdint ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值