c ++错误:无法匹配调用`(std :: string)(const char [4])'

时间:2014-11-25 17:59:44

标签: c++

我是c ++的新手,之前我曾经用Python编程。我正在学习如何定义一个 在c ++中的函数,我假设它有点像python def函数。我一直收到错误:

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

我正在使用dev 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')

5 个答案:

答案 0 :(得分:4)

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

这不是声明一个功能。这是一个名为subfunction的变量,它带有构造函数参数字(另一个字符串),后跟一个打印变量字的无关代码块。你只是调用字符串的复制构造函数意味着子函数和word是相等的字符串。

应该注意的是a)你不能在另一个内部定义一个函数,b)在C ++中函数参数需要一个类型。 I've fixed your code for you

#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;
}

P.S:main()不能自称。如果你打算多次调用它,直到它成功,或者在一个单独的递归函数中断掉你的逻辑使用while循环。

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

答案 1 :(得分:3)

无法在其他功能中定义功能。拉出定义(并删除在函数定义后未使用的尾随;):

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

int main()
{
    //...
}

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

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

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

答案 2 :(得分:2)

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

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

int main(){
    subfunction("abc");
    return 0;
}

另一件事是类型不像python中的动态,你需要显式声明你的类型。原始代码所做的实际上是定义一个名为string的{​​{1}}类型的变量,然后是一个不相关的代码块,但不是函数。正如你在this question中看到的那样,大括号可以定义一个作用域的块,你可能习惯于从python中具有特定含义的空格,但在c ++中,大括号并不意味着它必须是一个函数。然后,当您执行subfunction时,它会尝试将subfunction("abc")键入的“abc”传递给const char*变量,该变量属于subfunction类型,这是不允许的,因此会给您错误

你的main返回类型也应该是一个整数main always returns int,但更重要的是你应该总是为函数返回正确的类型:

string

习惯于寻找合适的类型,然后返回正确的类型。 现在你再次调用main,这几乎肯定不是你想要的。

答案 3 :(得分:0)

明智地说,语法上存在一些差异。 并且您已将该函数放在函数内部的main函数内。

#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;
        }

答案 4 :(得分:0)

如前所述,您无法在另一个函数中定义函数。因此,您需要在main之外移动子功能。其次,放一个&#34 ;;&#34;在任何结束时,c ++都标志着该语句的结束。那么,字符串子功能(word);是一个声明,并在下一行&#34; {&#34;标志着一个单独范围的开始。

另外,我很确定你不能从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;
}