C++错误:呼叫不匹配
我正在尝试在 C++ 中编译以下代码,
string initialDecision ()
{
char decisionReviewUpdate;
cout << "Welcome. Type R to review, then press enter." << endl;
cin >> decisionReviewUpdate;
// Processing code
}
int main()
{
string initialDecision;
initialDecision=initialDecision();
//ERROR OCCURS HERE
// More processing code
return 0;
}
就在它显示“此处发生错误”的地方,我在编译时收到以下错误:“错误:调用'(std::string) ()'时不匹配。如何才能我解决这个问题?
I'm trying to compile the following code in C++
string initialDecision ()
{
char decisionReviewUpdate;
cout << "Welcome. Type R to review, then press enter." << endl;
cin >> decisionReviewUpdate;
// Processing code
}
int main()
{
string initialDecision;
initialDecision=initialDecision();
//ERROR OCCURS HERE
// More processing code
return 0;
}
Right where it says "Error occurs here", I get the following error while compiling: "Error: No Match for Call to '(std::string) ()'. How can I resolve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不要给字符串和函数使用相同的名称,错误就会消失。
当您声明具有相同名称的局部变量时,编译器“忘记”有一个具有该名称的函数。
Don't give your string and your function the same name, and the error will go away.
The compiler has "forgotten" that there is a function with that name, when you declare a local variable with the same name.
局部变量隐藏了全局函数的名称。最好重命名局部变量,但还有范围运算符可以让您专门访问全局名称:
The local variable shadows the name of the global function. It is best to rename the local variable, but there is also the scope operator which lets you specifically access the global name:
这在 C++ 中称为“名称隐藏”。在此特定示例中,您声明一个局部变量,该变量与命名空间范围中的函数同名。在该变量的声明点之后,该函数将变得隐藏,每次您提到“initialDecision”名称时,编译器都会正确地假设您正在引用该变量。由于无法将函数调用运算符“()”应用于“string”类型的变量,因此编译器会发出错误消息。
在许多情况下,为了引用隐藏名称,您可以使用范围解析运算符“::”。例如,请参阅 UncleBens 的回复。
This is called "name hiding" in C++. In this particular example, you are declaring a local variable, which has the same name as a function in namespace scope. After the point of declaration of that variable the function becomes hidden, and every time you mention the 'initialDecision' name the compiler will rightfully assume that you are referring to the variable. Since you can't apply the function call operator '()' to a variable of type 'string', the compiler issues the error message.
In many cases in order to refer to hidden names you can use the scope resolution operator '::'. See UncleBens response, for example.
尝试重命名变量以使其与函数名称不匹配。
Try renaming the variable to not match the name of the function.
问题是您将名称initialDecision 重复作为变量和函数。这让编译器非常困惑。尝试将变量重命名为其他名称;然后它就会起作用。
The problem is you are repeating the name initialDecision as both a variable and a function. This confuses the compiler greatly. Try renaming the variable to something else; it will then work.