C++错误:呼叫不匹配

发布于 2024-08-06 20:39:25 字数 470 浏览 4 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

巷子口的你 2024-08-13 20:39:26

不要给字符串和函数使用相同的名称,错误就会消失。

当您声明具有相同名称的局部变量时,编译器“忘记”有一个具有该名称的函数。

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.

闻呓 2024-08-13 20:39:26

局部变量隐藏了全局函数的名称。最好重命名局部变量,但还有范围运算符可以让您专门访问全局名称:

initialDecision = ::initialDecision();

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();
你怎么这么可爱啊 2024-08-13 20:39:26

这在 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.

要走干脆点 2024-08-13 20:39:26

尝试重命名变量以使其与函数名称不匹配。

Try renaming the variable to not match the name of the function.

云淡风轻 2024-08-13 20:39:26

问题是您将名称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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文