Neko
Neko

Reputation: 51

Overload Function Error Messages

This program is supposed to check if a string is a palindrome by using only one variable and any number of stacks. It is giving me a number of error messages relating to overloaded functions, but I don't see where I overloaded it. Or did I declare the stacks wrong? I commented in the error messages after the lines of code they referred to. Thanks!

int main()
{
    stack <char> stackA;
    stack <char> stackB;
    stack <char> stackC;

    char avariable;
    cout << "Enter a string: ";
    avariable = cin.get();
    while(avariable != '\n')
    {
        if(avariable != ' ')
        {

            stackA.push(avariable);
            stackB.push(avariable);
            avariable = cin.get();
        }}

    stackC.push('$');

    while(!stackB.empty())
    {
        avariable = stackB.top; //error, cannot resolve overloaded function 'top' based on conversion to type 'char'
        stackC.push(avariable);
        stackB.pop; //statement cannot resolve address of overloaded function
    }

    avariable = '$';
    stackC.push('$');

    while(!stackA.empty())
    {
        if(avariable == stackC.top) //invalid operands of type 'char' and <unresolved overloaded function type>' to binary 'operator=='
        {
            avariable = stackA.top; //cannot resolve overloaded function 'top' based on conversion to type 'char'
            stackA.pop; //cannot resolve address of overloaded function 
            stackC.pop; //cannot resolve address of overloaded function 
        }
        else
        {
            cout << "The string of characters entered is not a palindrome." << endl;
        }
    }

    if (stackC.top == '$') //invalid operands of type 'char' and <unresolved overloaded function type>' to binary 'operator=='
    {
        cout <<"The string of characters entered is a palindrome." << endl;
    }

}

Upvotes: 1

Views: 1852

Answers (2)

Shoe
Shoe

Reputation: 76240

The std::stack template class only has member functions top() and pop() defined. You are trying to access them as public member objects, instead. Function calls require the use of parenthesis even if no arguments are passed.

Just replace all the occurrences of .top and .pop to .top() and .pop() respectively, and your program should at least compile.

Also please get used to the std:: prefix instead of including the entire std namespace with using namespace std. It will save you potential troubles, in the long run.

Upvotes: 2

Joseph Mansfield
Joseph Mansfield

Reputation: 110658

To call a function, you need parentheses:

stackB.top()
stackB.pop()

As both top and pop take no arguments, the parentheses are empty but still required.

This is no different to where you have done stackA.empty() or cin.get() for example.

Upvotes: 1

Related Questions