简体   繁体   English

错误。 C ++。 ')'令牌之前的预期主要表达式

[英]Error. C++. Expected primary-expression before ')' token

I wrote a simple program and met an error in switch . 我写了一个简单的程序,在switch中遇到错误。 What is wrong? 怎么了?

Error: expected primary-expression before ')' token 错误:“)”标记之前的预期主表达式

#include <iostream>
#include <list>
using namespace std;

int main() {
    list<string> myList;

    string s;
    while (true) {
        cin >> s;
        switch(s) {
            case "quit":
            break;

            default:
            myList.push_back(s);
            break;
        }
    }
}

Thanks. 谢谢。

The real problem is here: 真正的问题在这里:

 switch(s) {

You cannot use strings in a switch case. 您不能在开关盒中使用strings

Alternative: 选择:

An if-else ladder. 一个if-else梯子。 Since you have only one case, use an if statement for it. 由于只有一种情况,请使用if语句。 For example: 例如:

if (s=="quit") {
    break;
} 
else 
    myList.push_back(s);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM