C++ 错误:对"(std::string) (const char[4])"的调用不匹配

c++ error: no match for call to `(std::string) (const char[4])'

本文关键字:调用 不匹配 char std 错误 string C++ const      更新时间:2023-10-16

我对c++很陌生,以前我曾经用Python编程。我正在学习如何定义C ++中的函数,我认为它有点像python def函数。我不断收到错误:

no match for call to `(std::string) (const char[4])`.

我正在使用开发 c++。这是我的代码:

#include <iostream>
int main()
{
    using namespace std;
    cout << "Enter a number: "; // ask user for a number
    int x;
    cin >> x; // read number from console and store it in x
    cout << "You entered " << x << endl;
    cout << "n" << "Enter 'a': ";
    string y;
    cin >> y;
    if (y=="a"){
       cout << "You typed 'a'!!!";
       int z;
       cin >> z;
       return 0;
    }
    else {
         cout << "You didn't type 'a'!!!" << endl;
    }
    string word;
    string subfunction(word);
    {
         cout << word << endl;
    }
    subfunction("abc");
    return main();
}

最后,您可能已经注意到subfunction的声明和定义。如果我用python编写这个,它将是:

def subfunction(word):
  print(word)
subfunction('abc')
string word;
string subfunction(word);
{
     cout << word << endl;
}

这不是声明函数。这是声明一个名为 subfunction 的字符串类型的变量,其中包含构造函数参数 word(另一个字符串),后跟一个打印变量 word 的不相关的代码块。你只是调用字符串的复制构造函数,意思是子函数和单词是相等的字符串。

应该注意的是,a)你不能在另一个函数中定义一个函数,b)在C++函数参数需要一个类型。我已经为您修复了您的代码

#include <iostream>
using namespace std;
void subfunction(string word)
{
    cout << word << endl;
}
int main()
{
    cout << "Enter a number: "; // ask user for a number
    int x;
    cin >> x; // read number from console and store it in x
    cout << "You entered " << x << endl;
    cout << "n" << "Enter 'a': ";
    string y;
    cin >> y;
    if (y=="a"){
       cout << "You typed 'a'!!!";
       int z;
       cin >> z;
       return 0;
    }
    else {
         cout << "You didn't type 'a'!!!" << endl;
    }
    string word;
    subfunction("abc");
    return 0;
}

PS:main()不能自称。如果您打算多次调用它直到成功,请在单独的递归函数中中断逻辑或使用 while 循环。

P.P.S:我已将subfunction的返回类型更改为void,因为它没有返回任何内容。

函数不能在其他函数中定义。拉出定义(并删除尾随;,因为它在函数定义后未使用):

void subfunction( string word )
{
     cout << word << endl;
}
int main()
{
    //...
}

我更改了函数的返回类型,因为它不返回任何内容。

此外,显式调用 main() 是非法的。如果您希望程序循环,请使用循环:

int main()
{
    while( true )
    {
        // ...
    }
}

看起来你来自python背景,所以我会指出c ++中的不同之处。在 c++ 中,您不能像您在此处尝试的那样在其他函数的主体中定义命名函数。要获得这种类型的行为,您可能需要使用 lambda 或其他相关功能,请参阅此问题以获取有关该主题的更多信息。要使程序按预期工作,最简单的方法是将subfunction移动到主循环之外:

void subfunction(string word){
     cout << word << endl;
}
int main(){
    subfunction("abc");
    return 0;
}

另一件事是类型不像在 python 中那样是动态的,你需要显式声明你的类型。原始代码实际上是定义一个名为 subfunctionstring 类型的变量,然后在它下面定义一个不相关的代码块,而不是一个函数。正如你在这个问题中看到的,大括号可以定义一个块一个范围,你可能习惯于从python中具有特定含义的空格,但在c ++中,大括号并不意味着它必须是一个函数的类似含义。然后后来当你这样做subfunction("abc")它试图将const char*键入的"abc"传递到subfunction变量中,该变量的类型是不允许的string,因此给了你错误。

此外,你从main返回的类型应该是一个整数,main总是返回int但更重要的是你应该始终为函数返回正确的类型:

int main(){
^^^
this is telling you to return an int

习惯于查找正确的类型,然后返回正确的类型。现在你又打电话给main,这几乎肯定不是你想要的。

如前所述,您不能在另一个函数中定义一个函数。因此,您需要将子函数移到 main 之外。其次,在任何 c++ 的末尾加上 ";" 标志着该语句的结束。所以,字符串子函数(单词);是一个语句,在下一行,"{"标记单独范围的开始。

另外,我很确定你不能从main返回main()。

#include <iostream>
string subfunction(word)
{
     cout << word << endl;
}
int main()
{
    using namespace std;
    cout << "Enter a number: "; // ask user for a number
    int x;
    cin >> x; // read number from console and store it in x
    cout << "You entered " << x << endl;
    cout << "n" << "Enter 'a': ";
    string y;
    cin >> y;
    if (y=="a"){
       cout << "You typed 'a'!!!";
       int z;
       cin >> z;
       return 0;
    }
    else {
         cout << "You didn't type 'a'!!!" << endl;
    }
    string word;
    subfunction("abc");
    return 0;
}

清除方面,语法上存在一些差异。并且您已将函数放在主函数中,该函数位于函数内。

#include <iostream>
#include <string>
using namespace std;
string subfunction(word);
    {
         cout << word << endl;
    }
int main()
{
    using namespace std;
    cout << "Enter a number: "; // ask user for a number
    int x;
    cin >> x; // read number from console and store it in x
    cout << "You entered " << x << endl;
    cout << "n" << "Enter 'a': ";
    string y;
    cin >> y;
    if (y=="a"){
       cout << "You typed 'a'!!!";
       int z;
       cin >> z;
       return 0;
    }
    else {
         cout << "You didn't type 'a'!!!" << endl;
    }
    string word = "abc";
    subfunction(word);
}

如果只想显示,则不需要返回类型。所以你的函数可以是

void subfunction(word);
        {
             cout << word << endl;
        }