c++;错误:对`(std::string)(const char[4])'; 我对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(); }

c++;错误:对`(std::string)(const char[4])'; 我对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(); },c++,C++,注意:main()无法调用自身。如果要多次调用此函数直到成功,请在单独的递归函数中中断逻辑或使用while循环 < P.P.S >:我已经将子函数< /> >的返回类型改为无效>代码>,因为它没有返回任何东西。 看起来你是来自Python背景,所以我会指出C++中不同的东西。在C++中,您不能定义其他函数中的命名函数,就像您在这里尝试的那样。要获得该类型的行为,您可能需要使用lambda或其他相关功能,请参阅以获取有关该主题的更多信息。要使程序按预期工作,最简单的方法是将子功能移动到主循环之外:

注意:
main()
无法调用自身。如果要多次调用此函数直到成功,请在单独的递归函数中中断逻辑或使用while循环


< P.P.S >:我已经将<代码>子函数< /> >的返回类型改为<代码>无效>代码>,因为它没有返回任何东西。

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

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

int main(){
    subfunction("abc");
    return 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);
}
#包括
#包括
使用名称空间std;
字符串子函数(word);
{

CUT

正如前面提到过的,不能在另一个函数中定义一个函数。因此,需要在子函数外移动子函数。其次,在任何C++结尾加上“;”标记该语句的结尾。因此,String子函数(Word)是一个语句,在下一行中,“{”标志着单独的范围的开始。

另外,我非常确定不能从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;
}
#包括
字符串子函数(word)
{

CUT看起来像是来自Python背景,不能在C++中的主体中放置一个函数。所以至少你必须在主循环之外移动<代码>子函数< />。计算机语言解决逻辑问题,然后可以应用到C++——不要使用你的另一种语言的知识来学习C++本身。请不要问我们在学习C++时遇到的每一个错误。先仔细阅读,然后再来。这里的软件问题是可以的,只要问题不是“我还没有学会语言语法”。C++也有lambda(因为C++ 11)。但是它们有另一种语法,看看@user3159253我知道,但是lambda是函数对象(functor)而不是函数。我的陈述在技术上是正确的。是的,我知道。我的评论更多的是对你的答案的补充,而不是更正。
void subfunction(string word){
     cout << word << endl;
}

int main(){
    subfunction("abc");
    return 0;
}
int main(){
^^^
this is telling you to return an int
void subfunction( string word )
{
     cout << word << endl;
}

int main()
{
    //...
}
int main()
{
    while( true )
    {
        // ...
    }
}
#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;
        }
#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;
}