【发布时间】:2014-01-07 16:16:26
【问题描述】:
我正在研究 Bjarne Stroustrup 的《C++ 编程语言》,但我一直停留在其中一个示例上。这是代码,除了空格差异和 cmets 之外,我的代码与书中的代码相同(第 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;
}
然而,当我在 Mac OS X 10.9 上使用 clang++ -std=c++11 -stdlib=libc++ -Weverything main.cpp 编译它时,出现以下错误:
main.cpp:24:9: error: expected expression
switch (t) {
^
main.cpp:32:6: error: expected ';' at end of declaration
}
^
;
真正的障碍是expected expression 错误,但expected ; 也有问题。我做了什么?
【问题讨论】:
-
您正试图在函数内部定义一个函数。你不能。
-
只要在你的主函数之外做...
标签: c++ c++11 enums switch-statement operator-overloading