跳過我不明白爲什麼我收到的錯誤:初始化「元素」是由「案例」標籤
initialization of 'element' is skipped by 'case' label.
可有人請向我解釋?
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語句。 – Rapptz
每個'case'不會引入一個新的範圍(只有'{}'塊會這樣做)。所以當你在一個case中聲明一個變量時,它應該放在它自己的塊中。 – Cameron