我正在阅读 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;
}
然而当我用它编译时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
}
^
;
真正的阻碍是expected expression
错误,但是expected ;
也是有问题的。我做了什么?
Traffic_light&operator++(Traffic_light&t) 是一个名为operator++的函数。每个函数应在任何其他函数之外定义。因此将运算符的定义放在main之前。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)