0

I have a project to compare the contents of two stacks and I am having issues with this function. I believe I have the rest of the program done correctly. I am getting errors on B.myCharacters.empty() (Expression must have class type) and B==B.myCharacters (no operator "==" matched these operands).

bool CharStack::IsEqual(CharStack & B) 
{
    if (B.empty()) 
    {
        cout << "Stack is empty" << endl;
        return false;
    } 
    else if (B.myCharacters.empty()) 
    {
        cout << "Stack is empty" << endl;
        return false;
    } 
    else if (B == B.myCharacters)
        return true;
}

Any help would be greatly appreciated.

Here is the header and driver. They were provided by the teacher for this project and I am not allowed to change them, even if there is a better way to do it.

#include <iostream>

#include <string>

using namespace std;

const int STACK_CAPACITY = 128;

typedef char StackElement;

class CharStack
{
private:
    char myCharacters[STACK_CAPACITY]; // STL stack of chars.
    int myTop;

public:
    CharStack();
    bool empty();
    void push(const StackElement & value);
    StackElement top() const;
    void pop();
    void StringToStack(const string & inStr);
    friend ostream & operator <<(ostream & out, const CharStack & CS);
    CharStack Reverse();
    bool IsEqual(CharStack & B);
};

Driver

#include <string>
#include <cassert>
#include "Header.h"

using namespace std;
//introduces namespace std

int main(void)
{
    ifstream in;
    string fileName, line[30];
    int i = 0;
    CharStack N, M, P;
    cout << "Enter file name for palindrome check: ";
    cin >> fileName;
    in.open(fileName.c_str());
    assert(in.is_open());
    while (!in.eof())
    {
        getline(in, line[i]);
        N.StringToStack(line[i]);
        cout << N << endl;
        P = N;
        M = N.Reverse();
        if (P.IsEqual(M))
            cout << "This line is a palindrome line" << endl;
        else
            cout << "This line is not a palindrome line" << endl;
        i++;
    }

    cout << "\nProgram ended normally.\n";
    system("pause");
}
user4581301
  • 29,019
  • 5
  • 26
  • 45
  • 1
    Please provide [mcve]. Without knowing what is `CharStack` nobody can help. – S.M. Feb 24 '18 at 13:52
  • 2
    Welcome on SO! Unfortunately, we cannot help you much, unless you post the code used, in particular the definition of `CharStack`. See [here](https://stackoverflow.com/help/mcve) for what is required. – Walter Feb 24 '18 at 13:53
  • 1
    Please also be aware that it makes sense to say that two empty stacks are equal. In your code snippet they are not. – pschill Feb 24 '18 at 13:56
  • You get an error about a missing operator because you haven't defined how to compare two different things (`B` and `B.mycharacters`). I.e. you need to define `CharStack::operator==(const T& otherCharacters) const`. (Also note that you probably do not want this operator in your actual solution. This is just the operator your current solution needs to not complain about this operator missing.) – tehhowch Feb 24 '18 at 14:09
  • May not be your problem this time, but sooner or later the bug described in this question will get you: [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – user4581301 Feb 24 '18 at 15:29
  • Oh smurf. Just read a bit more. Since `while (!in.eof())` is in code provided by the instructor, you have two real choices. 1. Quietly and privately point out to the instructor that this is a common bug and that they should be using `while (getline(in, line[i])` instead. Use the link in the above comment to help you explain the problem. Option 2 is just carry on, pass the class, and check what you are being taught against a second source. Use option 1 if the instructor is open to correction. Use option 2 if you worry that correcting the instructor, even in private, will damage your grade. – user4581301 Feb 24 '18 at 15:45
  • Other notes: `using namespace std; //introduces namespace std` is either not true or misleading. One could say that it introduces the `std` namespace into the global namespace, taking everything in the `std` namespace and placing it into the global namespace. Read [Why is “using namespace std” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) to learn some of the reasons why you DON'T want to do this. – user4581301 Feb 24 '18 at 16:08
  • `char myCharacters[STACK_CAPACITY]; // STL stack of chars.` No it isn't. This is just a dumb old array. Most people would consider `std::stack` to be a STL stack, but even this is not strictly true. To get an STL stack you have to go back about 20 years to before the [Standard Template Library](https://en.wikipedia.org/wiki/Standard_Template_Library) was incorporated (in spirit but not quite implementation) into the C++ standard library (the `std` namespace). Today when someone is talking about the STL, they usually mean [the `std` containers.](http://en.cppreference.com/w/cpp/container) – user4581301 Feb 24 '18 at 16:15

1 Answers1

2

Assuming your CharStack internally keeps the characters in a std::string, i.e.

class CharStack
{
    std::string myCharacters;    // internal data
  public:
    bool IsEqual(CharStack const&) const;
    /* ... */
};

and IsEqual() should return if two stacks are identical, then the implementation is simply

bool CharStack::IsEqual(CharStack const&other) const
{
    return myCharacters == other.myCharacters;
}

This compares an empty and a non-empty stack as not equal, but two empty stacks as equal, which arguably is the correct behaviour. If you want two empty stacks to be not equal you can

bool CharStack::IsEqualNonEmpty(CharStack const&other) const
{
    return !myCharacters.empty() && myCharacters == other.myCharacters;
}

Note also the various uses of the keyword const.

Walter
  • 40,885
  • 16
  • 97
  • 176
  • I also have to make sure the stacks are not empty before trying to compare them though. – Jeffrey Smith Feb 24 '18 at 14:31
  • What do you mean? How are they supposed to be compared if empty? See also my edit. – Walter Feb 24 '18 at 18:42
  • It is not supposed to compare them if they are empty. It is supposed to return false and then show an error message if one or both of the stacks are empty. It is almost like a precondition for comparing them, but it is to be done inside the function. – Jeffrey Smith Feb 24 '18 at 19:37
  • makes no sense. – Walter Feb 24 '18 at 19:59