【问题标题】:c++ error: no match for call to `(std::string) (const char[4])'c++ 错误:对 `(std::string) (const char[4])' 的调用不匹配
【发布时间】:2025-12-26 01:20:13
【问题描述】:

我对 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')

【问题讨论】:

  • 看来你是python背景的,你不能在c++的main体中放一个函数。因此,您至少必须将subfunction 移到主循环之外。此外,出于各种原因,最好改掉using namespace std 的习惯。
  • 一些建议 -- 使用您对另一种计算机语言的知识来解决可以应用于 C++ 的逻辑问题 -- 不要使用您对另一种语言的知识来学习 C++ 本身。
  • return main();?!?认真的吗?
  • 请不要向我们询问您在学习 C++ 时遇到的每一个错误。先读这本书——仔细,然后来。软件问题在这里发帖是可以的,只要问题不是“我还没有学过语言语法”即可。

标签: c++


【解决方案1】:
string word;
string subfunction(word);
{
     cout << word << endl;
}

这不是声明一个函数。这是声明一个名为 subfunction 的变量,其类型为 string,构造函数参数为 word(另一个字符串),然后是一个不相关的代码块,用于打印变量 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,因为它没有返回任何内容。

【讨论】:

  • C++ 也有 lambdas(自 C++11 起),但它们有另一种语法,看看 en.cppreference.com/w/cpp/language/lambda
  • @user3159253 我知道,但是 lambda 是函数对象(函子)而不是函数。我的陈述在技术上是正确的。
  • 是的,我知道。我的评论更多的是对您的回答的补充而不是更正。
【解决方案2】:

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

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

int main()
{
    //...
}

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

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

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

【讨论】:

    【解决方案3】:

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

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

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

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

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

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

    【讨论】:

      【解决方案4】:

      如前所述,您不能在另一个函数中定义一个函数。因此,您需要将子功能移到主功能之外。其次,放一个“;”在任何 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;
      }
      

      【讨论】:

        【解决方案5】:

        declearation 明智地在语法上有一些差异。 而且你已经把函数放在了主函数里面,也就是函数里面的函数。

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

        【讨论】:

          最近更新 更多