【发布时间】:2013-01-18 03:10:39
【问题描述】:
我不明白为什么会出现错误:
“元素”的初始化被“案例”标签跳过。
谁能给我解释一下?
void LinkedList::process_example(int choice) {
switch(choice) {
case 1:
cout << endl << endl << "Current S = ";
this->printSet();
cout << "Enter an element :";
char* element = "lol";
//cin>>element;
cin.clear();
cin.ignore(200, '\n');
this->Addelementfromback(element); //error is here
cout << endl << endl << "Current S = ";
this->printSet();
break;
case 2:
this->check_element();
break;
case 3:
cout << endl << endl;
cout << "Current Set S = ";
this->printSet();
cout << endl << "S has ";
int count = this ->check_cardinality();
cout << count << " elements";
break;
}
}
【问题讨论】:
-
那个错误很明显,switch语句的使用也很奇怪。
-
每个
case都不会引入新的作用域(只有{ }块会这样做)。所以当你在一个 case 中声明一个变量时,它应该放在它自己的块中。