no match for call to '(std::string {aka std::__cxx11::basic_string<char>}) ()'

4.3k Views Asked by At

I am making a project in C++ and it gives the following error:

in function 'int main()': Line 35; Col 12; [Error] no match for call to '(std::string {aka std::__cxx11::basic_string}) ()'

This is Line 35:

This is password():

int password() {
    string password = "...";
    string pass2 = "...";
    string val1;
    cout << "Would you like to enter Password 1 or 2? ";
    cin >> val1;
    if (val1 == "1") {
        cout << "Enter Password: ";
        cin >> val1;
        if (val1 == password) {
            cout << "Success\n";
        } else {
            return EXIT_FAILURE;
        }
    }
}

Can anybody explain and fix this?

2

There are 2 best solutions below

0
On

It seems your function password was hidden by declaration of a variable with the same name of the type std::string in some scope of the function main where you are using the expression password().

So in an expression statement like this

password():

there is used an object of the type std::string for which the function call operator is not defined.

Either rename the function or the declared variable in main with the same name as the function name.

Another way to resolve the problem is to use a qualified name for the function call. For example if the function is declared in the global namespace then you can call it like

::password():

Pay attention to that your function does not return a value in all paths of its execution. Also the variable pass2

string pass2 = "...";

is not used in the function.

1
On

Thank you
Never realized password std::string and function were the same.