C++ 错误:呼叫不匹配
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1548658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ Error: No Match for Call
提问by waiwai933
I'm trying to compile the following code in 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;
}
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?
就在它说“此处发生错误”的地方,我在编译时收到以下错误:“错误:调用 '(std::string) ()' 时不匹配。我该如何解决这个问题?
回答by Mark Rushakoff
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.
当您声明具有相同名称的局部变量时,编译器“忘记”了具有该名称的函数。
回答by UncleBens
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:
局部变量掩盖了全局函数的名称。最好重命名局部变量,但还有作用域运算符可让您专门访问全局名称:
initialDecision = ::initialDecision();
回答by AnT
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.
这在 C++ 中称为“名称隐藏”。在此特定示例中,您要声明一个局部变量,该变量与命名空间范围内的函数具有相同的名称。在该变量的声明点之后,该函数变为hidden,每次您提到“initialDecision”名称时,编译器都会正确地假定您指的是该变量。由于您无法将函数调用运算符“()”应用于“字符串”类型的变量,因此编译器会发出错误消息。
In many cases in order to refer to hidden names you can use the scope resolution operator '::'. See UncleBens response, for example.
在许多情况下,为了引用隐藏名称,您可以使用范围解析运算符“::”。例如,请参阅 UncleBens 的回复。
回答by Yuliy
Try renaming the variable to not match the name of the function.
尝试重命名变量以使其与函数名称不匹配。
回答by Hyman Lloyd
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.
问题是您将名称 initialDecision 作为变量和函数重复。这让编译器非常困惑。尝试将变量重命名为其他名称;然后它会起作用。

