- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有人可以帮助解决以下编译器错误吗?我正在尝试在 C++ 中创建类似 Java 的枚举,但在 Visual Studio 2015 中遇到编译器错误。
我正在使用这些类似 Java 的 Enum 类作为结构的成员,我希望 sizeof(MessageType) 与 Value 类型相同。我知道这将要求我从类中删除 mValueStr 成员,但是我将无法从字符串中查找值。任何关于我如何能够实现这一目标的建议将不胜感激。
Java 枚举中真正巧妙的事情之一是能够使用字符串名称或值查找枚举类型。为此,我向我的枚举类添加了 2 个方法,以从这些字符串或值中反向查找枚举。不幸的是,下面的方法有指示的波浪线
static MessageType valueOf(const int& rVal) {
for (const auto& next : getValues()) {
if (next == rVal) {
~~
return next;
}
}
throw std::invalid_argument(
"Illegal Argument: " + rVal);
}
编译器报错如下,我不太明白:
1>c:\main\dlmu\caclient\udpclient\cmc.h(123): error C2678: binary '==': no operator found which takes a left-hand operand of type 'const MessageType' (or there is no acceptable conversion)
1> c:\main\dlmu\caclient\udpclient\cmc.h(123): note: could be 'built-in C++ operator==(MessageType::Value, int)'
1> c:\main\dlmu\caclient\udpclient\cmc.h(123): note: while trying to match the argument list '(const MessageType, const int)'
1> UDPClient.cpp
1>c:\main\dlmu\caclient\udpclient\cmc.h(123): error C2678: binary '==': no operator found which takes a left-hand operand of type 'const MessageType' (or there is no acceptable conversion)
1> c:\program files (x86)\windows kits\8.1\include\shared\guiddef.h(192): note: could be 'bool operator ==(const GUID &,const GUID &)'
1> c:\main\dlmu\caclient\udpclient\cmc.h(123): note: while trying to match the argument list '(const MessageType, const int)'
使这种类型的功能正常工作的关键是将整数转换运算符用于值类型(在本例中为 e 位枚举值)
class MessageType {
public:
enum class Value : uint8_t {
undefined = 0, membersystem = 10
};
static const MessageType Undefined, MemberSystem;
// integral operator cast for switch statements (cast to named enum)
inline operator const Value() const {
return mValue;
}
// strict weak ordering for set ops
inline bool operator<(const MessageType& rhs) const {
return mValue < rhs.mValue;
}
// serialized version of the enum
inline std::string getStringVal() const {
return mStringVal;
}
static const std::set<MessageType>& getValues() {
static std::set<MessageType> gValues;
if (gValues.empty()) {
gValues.insert(Undefined);
gValues.insert(MemberSystem);
}
return gValues;
}
static MessageType valueOf(const int& rVal) {
for (const auto& next : getValues()) {
if (next == rVal) {
return next;
}
}
throw std::invalid_argument(
"Illegal Argument: " + rVal);
}
static MessageType valueOf(const std::string& rStringVal) {
for (const auto& next : getValues()) {
if (next.getStringVal() == rStringVal) {
return next;
}
}
throw std::invalid_argument(
"Illegal Argument: " + rStringVal);
}
private:
MessageType(const Value& rValue, const std::string& rStringVal)
: mValue(rValue)
, mStringVal(rStringVal)
{}
Value mValue;
std::string mStringVal;
};
真正奇怪的是,就在这个定义之前,我有一个类似的类,它工作正常,没有波浪线,如下所示:
class DiscreteStatus {
public:
enum Value : uint8_t {
normaloperation = 0, nocomputeddata = 1, functionaltest = 2, failurewarning = 3
};
static const DiscreteStatus NormalOperation, NoComputedData, FunctionalTest, FailureWarning;
// integral operator cast for switch statements (cast to named enum)
inline operator const Value() const {
return mValue;
}
// strict weak ordering for set ops
inline bool operator<(const DiscreteStatus& rhs) const {
return mValue < rhs.mValue;
}
// serialized version of the enum
inline std::string getStringVal() const {
return mStringVal;
}
static const std::set<DiscreteStatus>& getValues() {
static std::set<DiscreteStatus> gValues;
if (gValues.empty()) {
gValues.insert(NormalOperation);
gValues.insert(NoComputedData);
gValues.insert(FunctionalTest);
gValues.insert(FailureWarning);
}
return gValues;
}
static DiscreteStatus valueOf(const int& rVal) {
for (const auto& next : getValues()) {
if (next == rVal) {
return next;
}
}
throw std::invalid_argument(
"Illegal Argument: " + rVal);
}
static DiscreteStatus valueOf(const std::string& rStringVal) {
for (const auto& next : getValues()) {
if (next.getStringVal() == rStringVal) {
return next;
}
}
throw std::invalid_argument(
"Illegal Argument: " + rStringVal);
}
private:
DiscreteStatus(const Value& rValue, const std::string& rStringVal)
: mValue(rValue)
, mStringVal(rStringVal)
{}
Value mValue;
std::string mStringVal;
};
实现文件(CPP)执行静态初始化如下:
const DiscreteStatus DiscreteStatus::NormalOperation(DiscreteStatus::Value::normaloperation, "NML");
const DiscreteStatus DiscreteStatus::NoComputedData(DiscreteStatus::Value::nocomputeddata, "NCD");
const DiscreteStatus DiscreteStatus::FunctionalTest(DiscreteStatus::Value::functionaltest, "FT");
const DiscreteStatus DiscreteStatus::FailureWarning(DiscreteStatus::Value::failurewarning, "FW");
const MessageType MessageType::Undefined(MessageType::Value::undefined, "Undefined");
const MessageType MessageType::MemberSystem(MessageType::Value::membersystem, "MemberSystem");
最佳答案
在这两种情况下,目的都是调用类型转换运算符以执行到相应的 Value
类型的转换。
在第一种情况下,您的 Value
定义如下:
enum class Value : uint8_t
这不像公共(public)继承那样工作,但更类似于私有(private)继承:它是一种implemented-in-terms-of 关系。客户端代码不得并且在您的情况下不能依赖于 Value
在内部实现为 uint8_t
这一事实。
然而,在第二种情况下,它是这样的:
enum Value : uint8_t
这可以被描绘成类似公共(public)继承的东西,这意味着客户端代码可以依赖并依赖于 Value
实际上是一个 uint8_t
这一事实。类型转换因此有效。
现在,您可以通过将第一个定义更改为 enum Value : uint8_t
来快速修复您的代码,但是有人想知道为什么您要提供一个 valueOf
函数无论如何都有一个整数参数。它打破了抽象。
一般来说,我强烈反对这整个方法。这似乎过于复杂,就像人们试图用语言 B 编写语言 A 时的情况一样。
在这种特殊情况下,Java 枚举和 C++11 枚举类之间的任何相似之处都只是表面的。归根结底,从 C++ 的角度来看,Java 枚举是不可复制的类,具有指向无法取消引用的垃圾收集实例的静态常量指针。您不想在 C++ 中模拟所有这些,您也不能,也不应该。
关于C++ Java like enum header 编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34777054/
我在让“@header”或任何其他@规则在ANTLR中工作时遇到麻烦。具有非常基本的语法,如下所示: grammar test; options { language = CSharp2;
我对来源和寄宿有疑问 我有一个ajax页面“Page A”,它将称为ajax提要“Page B” 我看到来自ajax调用的“页面B”的请求 header 具有源“http://mydomain.com
我在 pandas 中使用了数据透视表并获得了所需的数据框格式,但现在我有两行标题。数据透视表后的结果数据框如下: scenario Actual Plan
我在 pandas 中使用了数据透视表并获得了所需的数据框格式,但现在我有两行标题。数据透视表后的结果数据框如下: scenario Actual Plan
我想在主机将它们发送到网络之前修改数据包头(IP 头、TCP 头)。 例如,如果我使用 firefox 进行浏览,那么我想拦截所有来自 firefox 的数据包并修改 IP/TCP header ,然
我的 header 内容被包装到#header 中,但是当我设置边框显示结构时,它显示我的#header 的内容出现在#header 本身之后。可能是什么问题?这是我的代码: #header { bo
我是一名 Web 开发人员,使用过 PHP 和 .NET。有一年多的 Web 工作经验,我一直无法彻底了解浏览器缓存功能,希望这里的 Web Gurus 可以帮助我。我心中的问题是: 浏览器实际上是如
伙计们,我有一个问题,我不知道如何在一个 header 中连接多个 header ,我们称它为“主 header ”并使用该 header 中的函数,例如 // A.h #include class
我有一个包含 SOAP 消息的 XMLHTTPRequest。 我想添加用于标识消息并将由 C# Web 服务使用的 guid。 GUID 的目标是识别特定用户,并应护送所有用户请求以在服务器上进行身
我一直在阅读粘性标题,这是我目前所发现的。第一个粘性 header 效果很好,但是当它遇到第一个 header 时,我如何向上滚动第一个 header 并使第二个 header 卡住? http://
我想将当前基于 TableView 的数据网格转换为新的 UICollectionView 类。 这就是我当前的网格的样子: 我的网格有两个标题: 年份(2006a、2007a 等)和 类型(“收入”
我目前正在使用 Apollo 服务器。我正在尝试在响应 header 中设置一个属性。并且此属性是从客户端 graphQL 请求 header 中检索的。 我在网上查了一下。并看到了诸如使用插件或扩展
我的 Controller 的方法需要设置一个标题,例如X-Authorization .创建新对象( store Action )后,我执行转发以显示新创建的对象( show Action ): $
我正在研究一些关于 VLAN 的事情,发现了 VLAN 标签 和 header 。 如果我们有标准 802.3 以太网帧 的 MTU(1518 字节), header 802.3 中包含什么? 另外,
我是放心和 Java 的新手,我正在尝试做一个非常基本的测试来检查 API 的响应是否为 200 ok。 谁能告诉我我需要在下面的脚本中更改什么才能传递多个 header Id、Key 和 ConId
在我的项目中,我需要知道 zlib header 是什么样的。我听说它相当简单,但我找不到 zlib header 的任何描述。 例如,它是否包含魔数(Magic Number)? 最佳答案 zlib
我正在使用 JMeter 测试 HTTP 服务器,该服务器接受并验证 APIKey 并在成功时返回一个有时限的 token 。如果我有 token ,我想发送一个 token ;如果没有,我想发送一个
以太网 header 是什么样的? 是吗: 1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|
我们的应用程序支持 CORS 配置 header 。我在两个不同的主机上分别配置了 testApp。两种设置都相互独立工作。host1 上的应用程序配置有 CORS header Access-Con
tlhelp32.h 不包含 windows.h 本身是有原因的吗?我一直在与大量的编译器错误作斗争,因为我在包含 tlhelp32.h 之后包含了 windows.h。这是设计决定还是出于什么原因?
我是一名优秀的程序员,十分优秀!