0

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?

user438383
  • 5,716
  • 8
  • 28
  • 43
thes_real
  • 1
  • 1
  • 1
  • 4
    Don't name your functions and your variables the same thing. Give them different names. – NathanOliver Apr 06 '22 at 12:39
  • 3
    Also, since you have declared your function as returning `int`, you must ensure that it does not simply finish execution without returning anything. For example, I see there is `return EXIT_FAILURE;`, but this only happens in one particular case, nothing is returned otherwise. This will cause undefined behavior. – heap underrun Apr 06 '22 at 12:53

2 Answers2

3

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

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

thes_real
  • 1
  • 1
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 08 '22 at 11:23