【问题标题】:C++ Enum in struct error for definition用于定义的结构错误中的 C++ 枚举
【发布时间】:2016-10-28 10:04:15
【问题描述】:

你好,我是 C++ 的新手。 对于我的项目,我需要使用类似于 C# 示例的枚举值。

我的结构

struct Rows
{
    string name;
    bool primaryKey;
    int lenght;
    string defined;
    MyLDC::Attributes attrib;
    bool Nullable;
    bool AutoIncrement;
    string comment;

};
Rows rw;
Rows* row = &rw;

MyLDC::属性

enum Attributes
{
    _BINARY = 0x26,
    _UNSIGNED = 0x27
};

我的示例函数

void MyLDC::CreateTable(string tablename,string primaryKey)
{

    //Simple Row Implementation
    row->name = "Example Row";
    row->AutoIncrement = true;
    row->primaryKey = true;
    row->comment = "Example Row";

    row->attrib = Attributes::_UNSIGNED;

我在 row->attrib = Attributes::_UNSIGNED; 上遇到错误;

不知道这个错误。 谁是正确的解决方案?

【问题讨论】:

  • 你得到什么错误?请在问题正文中包含完整且未经编辑的完整错误。
  • 与您的错误无关,以下划线开头并后跟大写字母的符号名称保留用于实现(编译器和标准库)。参见例如this old SO answer for more information.
  • 错误:属性'不是类或命名空间|

标签: c++ struct enums


【解决方案1】:
enum Attributes
{
    _BINARY = 0x26,
    _UNSIGNED = 0x27
};
...
row->attrib = Attributes::_UNSIGNED;

我的通灵调试能力;)告诉我问题是_UNSIGNED 符号不在Attributes 的范围内,所以你应该可以这样做:

row->attrib = _UNSIGNED;

对于作用域枚举,您可能需要使用 C++11 的 enum class

P.S. 另请注意,_Upper(下划线后跟大写字母)名称​​为实现保留,不应在您的代码中使用。

【讨论】:

  • @JoeMartiniello:很高兴能提供帮助。我以前也去过那里:)
  • 对不起,我没有得到。它似乎对我有用:ideone.com/4KyrH6我在做什么不同?
猜你喜欢
  • 2012-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-21
  • 2013-06-19
  • 1970-01-01
  • 2017-01-23
  • 2016-01-16
相关资源
最近更新 更多