initialization of 'element' is skipped by 'case' label
本问题已经有最佳答案,请猛点这里访问。
我不明白为什么会出错:
initialization of 'element' is skipped by 'case' label.
有人能给我解释一下吗?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | 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, ' '); 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; } } |
试着用
1 2 3 4 5 6 7 | case 1: { cout << endl << endl <<"Current S ="; this->printSet(); // and other mess } break; |
您应该将所有这些语句放到函数中,保持
1 2 3 4 5 6 | case 1: initializeElement(); break; case 2: doSomethingElse(); break; |
参见链接
当一个变量在一个
您只需在