是什么破坏了这个代码
What breaks this code?
很抱歉问了一个标题如此"有意义"的问题,但我不明白编译器在抱怨什么,代码刚才运行得很好,我添加了一个额外的方法,一切都坏了。删除最后的更改并没有修复它。
class NodeWrapper : public QObject {
Q_OBJECT
Q_ENUMS(NodeType NodeStatus)
Q_PROPERTY(NodeType type READ type WRITE setType NOTIFY typeChanged)
Q_PROPERTY(QString typeString READ typeString NOTIFY typeChanged)
Q_PROPERTY(NodeStatus status READ status WRITE setStatus NOTIFY statusChanged)
public:
NodeWrapper(QObject * parent = 0) : QObject(parent) { }
enum NodeType {
T_NODE,
T_FOLDER
};
enum NodeStatus {
S_OK,
S_WARNING,
S_ERROR
};
// type
inline NodeType type() { return _type; }
inline QString typeString() { return metaObject()->enumerator(0).key(type()); }
inline void setType(NodeType v) {
if (_type != v) {
_type = v;
emit typeChanged();
}
}
// status
inline NodeStatus status() { return _node.getStatus(); }
inline void setStatus(NodeStatus v) {
if (_node.getStatus() != v) {
_node.setStatus(v);
emit statusChanged();
}
}
signals:
void typeChanged();
void statusChanged();
private:
NodeType _type;
Node _node;
};
错误从NodeStatus
枚举的S_OK
成员开始:
error: expected identifier before '(' token
S_OK,
^
error: expected '}' before '(' token
error: expected ')' before numeric constant
S_OK,
^
error: 'NodeType' does not name a type
inline NodeType type() { return _type; }
^
error: 'metaObject' was not declared in this scope
inline QString typeString() { return metaObject()->enumerator(0).key(type()); }
^
error: 'type' was not declared in this scope
inline QString typeString() { return metaObject()->enumerator(0).key(type()); }
^
...
猜测您无意中在该定义之前包含了<windows.h>
,该定义通过#define将S_OK重新定义为数字,从而导致枚举中出现编译错误。全局名称空间中的这种混乱是C兼容性的不幸结果,在实践中,最安全的选择是将字段(S_SUCCESS?)重命名为
S_OK被定义为HRESULT为零(winerror.h)。请尝试其他前缀,如NS_OK或nsOK。
如果你在谷歌上搜索S_OK define
,你会发现在一些窗口标题中,它被定义为(HRESULT)0x00000000
——这解释了编译器抱怨parens和数字常量的原因。
你能做些什么来消除这个错误:
- 不要在要使用该常量的地方包含WinAPI头,或者
- 将其重命名为其他名称。可能会丢失
S_
前缀或使用其他前缀
后者更有可能工作,因为您经常会遇到WinAPI标头。也许甚至QT也会把你的类暴露给他们,因为S_OK
是一个邪恶的#define
,你几乎没有机会对抗它
相关文章:
- 任何人都可以告诉我我的 C++ 代码出了什么问题?
- 代码占用了我输入的一半
- 我无法找出我的代码出了什么问题
- wxWidgets mac剪贴板在3.1.3上坏了?
- 计时器坏了或者其他什么的
- 为什么我的代码返回了错误的平方值?C++
- 运行简单的.oct文件时Octave崩溃,我的代码出了什么问题?
- 我无法弄清楚我的代码出了什么问题
- boost::p rocess::env 在 ubuntu 19.04 上坏了?
- 堆排序,我无法弄清楚我的代码出了什么问题,输出顺序不正确
- 为什么这段代码崩溃了
- 课堂上的一行,使整个应用程序坏了. 0xC000005错误
- 为什么我的代码给了我错误的结果?
- 我似乎无法理解我的代码出了什么问题,我无法获得输出
- 我的合并sort.h文件中的代码出了什么问题
- 为什么我的代码崩溃了,浮点异常
- PQXX简单代码崩溃了
- 这段代码出了什么问题?它在第二个 cin 可以执行之前终止
- 我的代码怎么了?带有QT的原始输入API
- 条件评估是否优化?这个代码坏了吗