在main()中使用枚举的typedef结构不起作用

时间:2014-07-22 00:59:02

标签: c++ struct enums typedef

这里凌晨4点,我真的放弃了,有人帮忙!

#include <iostream>
using namespace std;

int d;

typedef struct my_data
{

    enum calling_func
    {
        TEST_A,
        TEST_B,
        TEST_C
    } val;

}letter_data;


int main() {
    letter_data l;
    l.val = TEST_A; // error: 'TEST_A' was not declared in this scope

    cout << "test" << endl;

    return 0;
}

我收到的错误是TEST_A未在此范围内声明。我是c ++的初学者,所以我承认如果不是更糟的话我就是傻瓜......

2 个答案:

答案 0 :(得分:3)

试试这个:

int main() {
    letter_data l;
    l.val = my_data::TEST_A; // error: 'TEST_A' was not declared in this scope

    cout << "test" << endl;

    return 0;
}

顺便说一下,在C ++中你不需要typedef,删除它。

编辑1:没有typedef

的示例
struct letter_data
{

    enum calling_func
    {
        TEST_A,
        TEST_B,
        TEST_C
    } val;

};


int main() {
    letter_data l;
    l.val = letter_data::TEST_A; // error: 'TEST_A' was not declared in this scope

    cout << "test" << endl;

    return 0;
}

答案 1 :(得分:2)

Clang产生一条非常易读的错误信息:

[8:10pm][wlynch@watermelon /tmp] clang++ red.cc 
red.cc:21:13: error: use of undeclared identifier 'TEST_A'; did you mean 'my_data::TEST_A'?
    l.val = TEST_A; // error: 'TEST_A' was not declared in this scope
            ^~~~~~
            my_data::TEST_A
red.cc:11:9: note: 'my_data::TEST_A' declared here
        TEST_A,
        ^
1 error generated.