当我尝试使用vector.size()时,它给出了以下错误:
In function 'int main()':
[Error] expected unqualified-id before '(' token
[Error] expected primary-expression before ')' token
[Error] lambda-expression in unevaluated context
[Error] expected identifier before numeric constant
In lambda function:
[Error] expected '{' before ')' token
Run Code Online (Sandbox Code Playgroud)
其余的向量函数都有效.我之前使用过这个功能而且它有效,现在我无法使用它.
这是代码:
while(1)
{
vector<string> frase;
string stringa;
getline(cin,stringa);
{
string temp;
temp.clear();
for(int i=0;i<stringa.length();i++)
{
if(stringa[i]!=' ')temp+=stringa[i];
else
{
frase.push_back(temp);
temp.clear();
}
}
frase.push_back(temp);
}
analizza(frase.data(), /*ERROR HERE*/ frase.size() /*ERROR HERE*/);
frase.clear();
for(int i=0;i<frase.size();i++)frase.pop_back();
}
Run Code Online (Sandbox Code Playgroud)
我能做什么?
您正尝试在此处声明具有运行时大小的数组:
string parole[frase.size()];
Run Code Online (Sandbox Code Playgroud)
C++不支持这一点.看起来您的编译器正在将其作为lambda表达式进行解释.
你根本不需要一个数组 - 只需使用frase.或者,如果您的函数确实需要将指针作为输入,请使用frase.data():
while(1)
{
vector<string> frase;
string stringa;
getline(cin,stringa);
{
//same as before
}
analizza(frase.data(), frase.size());
frase.clear();
}
Run Code Online (Sandbox Code Playgroud)