简体   繁体   中英

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

I am very new to c++, previously I used to program in Python. I am learning how to define a function in c++, I assume it is a bit like the python def function. I keep getting the error:

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

I am using dev c++. Here is my code:

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

At the end, you might have noticed the declaration and definition of subfunction . If I was writing this in python it would be:

def subfunction(word):
  print(word)

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

This is not declaring a function. This is declaring a variable named subfunction of type string with constructor parameter word (another string), followed by an unrelated block of code that prints the variable word. You're just calling the copy constructor of string meaning subfunction and word are equal strings.

It should be noted that a) you cannot define a function inside another one, b) in C++ function parameters need a type. 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;
}

PS: main() cannot call itself. If you meant to call this multiple times until it succeeds either break off your logic in a separate recursive function or use a while loop.

PPS: I've changed the return type of subfunction to void since it was not returning anything.

Functions cannot be defined inside other functions. Pull out the definition (and remove the trailing ; as it is not used after a function definition):

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

int main()
{
    //...
}

I changed the return type of the function since it does not return anything.

Also, it is illegal to explicitly call main() . If you want your program to loop, use a loop:

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

It looks like you are from a python background so I'll point out things that are different in c++. In c++ you cannot define named functions within the body of other functions like you are attempting to do here. To get that type of behavior you might want to use a lambda or other related functionality, see this question for more on that topic. The easiest thing to do to make your program work as intended would be to move the subfunction to outside of the main loop:

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

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

The other thing is that types are not dynamic like in python, you need to explicitly declare your types. What the original code did was actually define a variable of type string called subfunction and then an unrelated block of code below it but not a function. As you can see in this question braces can define a block a scope, you might be used to whitespace having a specific meaning from python but in c++ braces don't imply a similar meaning that it must be a function. Then later when you did subfunction("abc") it tried to pass the const char* typed "abc" into subfunction variable which is of type string which is not allowed and hence gave you your error.

Also your return type from main should be an integer, main always returns int but the more important thing is that you should always be returning the correct type for the function:

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

Get used to looking for the right types then returning the correct types. Right now you are calling main again which is almost certainly not what you want.

declearation wise there are some difference in syntax. And you have put the function inside the main function which is function inside function.

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

if you only want to display you don't need a return type. So your function can be

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

As it has been mentioned, you cannot define a function within another function. Therefore, you need to move subfunction outside of main. Secondly, putting a ";" at the end of anything c++ marks the end of that statement. So, string subfunction(word); is one statement and on the next line the "{" marks the beginning of a separate scope.

Also, I am pretty sure you cannot return main() from 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM