无法在C++中定义++运算符,这里有什么问题?

Jon*_*han 1 c++ enums operator-overloading switch-statement c++11

我正在研究Bjarne Stroustrup的C++编程语言,我坚持其中一个例子.这是代码,除了空格差异和注释,我的代码与书中的代码相同(第51页).

enum class Traffic_light { green, yellow, red};
int main(int argc, const char * argv[])
{
    Traffic_light light = Traffic_light::red;
//    DEFINING OPERATORS FOR ENUM CLASSES
//    enum classes don't have all the operators, must define them manually.
    Traffic_light& operator++(Traffic_light& t) {
        switch (t) {
            case Traffic_light::green:
                return t = Traffic_light::yellow;
            case Traffic_light::yellow:
                return t = Traffic_light::red;
            case Traffic_light::red:
                return t = Traffic_light::green;
        }
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

然而,当我clang++ -std=c++11 -stdlib=libc++ -Weverything main.cpp在Mac OS X 10.9上编译它时,我收到以下错误:

main.cpp:24:9: error: expected expression
        switch (t) {
        ^
main.cpp:32:6: error: expected ';' at end of declaration
    }
     ^
     ;
Run Code Online (Sandbox Code Playgroud)

真正的baffeler是expected expression错误,但expected ;也是有问题的.我做了什么?

Vla*_*cow 9

Traffic_light和operator ++(Traffic_light&t)是一个名为operator ++的函数.每个功能都应在任何其他功能之外定义.因此,在main之前放置运算符的定义.