C ++未声明标识符 - 错误C2065

时间:2014-08-26 12:52:42

标签: c++

这很可能是一个简单的错误,但我只需要知道如何定义colour以及如何在此程序中执行此操作。 (我创建了一个colour.h文件,并将其放入Source Files文件夹中,但尚未写入任何内容。)

错误说明error C2065: 'colour': undeclared identifier

这是发生错误的代码部分:

enum class traffic_light { green, yellow, red };
traffic_light light = traffic_light::red;


traffic_light& operator++(traffic_light &colour);

    switch (colour) {
    case traffic_light::green:{
                                  cout << "green\n";
                                  return colour = traffic_light::yellow;
    }
    case traffic_light::yellow: {
                                    cout << "yellow\n";
                                    return colour = traffic_light::red;
    }
    case traffic_light::red:{
                                cout << "red\n";
                                return colour = traffic_light::green;
    }
}

traffic_light next = ++light;
    cout << " << next << \n";
}

错误发生在第13行:switch (colour) {

编辑:traffic_light&amp; amp;之后的分号operator ++(traffic_light&amp; color);是必需的,因为这段代码是main()的一部分。删除main,表示不需要分号,但会出现链接器错误。

1 个答案:

答案 0 :(得分:0)

您正在关闭您的方法; 如果让缩进帮助你

,编程会更容易
traffic_light& operator++(traffic_light &colour)//;<-- THIS was the real culprit
{ //<-- and now the switch is a part of the operator function body
    switch (colour) {
        case traffic_light::green: {
            cout << "green\n";
            return colour = traffic_light::yellow;
        }
        case traffic_light::yellow: {
            cout << "yellow\n";
            return colour = traffic_light::red;
        }
        case traffic_light::red: {
            cout << "red\n";
            return colour = traffic_light::green;
        }
    }
    // also, what do you intend with the following?
    /*traffic_light next = ++light;
    cout << " << next << \n";*/
    // if we got here there's an error, maybe a new light type?
    // return red to be safe
    return colour = traffic_light::red;
}