Can someone help me determine if 2 stacks are the same? I am fairly new to stacks and need help. My header file is also below. Currently I am getting an error message using "==" and "!=" in my if statement. If someone can help, I will appreciate it! Thanks


error messages:

12 no match for 'operator==' in 's1 == s2'
21 no match for 'operator!=' in 's1 != s2'
26 no match for 'operator==' in 's1 == s2'
37 no match for 'operator!=' in 's1 != s2'


Expected Output or similar

empty stacks are equal
s1: 20 10
s2: 10
s1 is not equal to s2
s2: 20 10
s1 is equal to s2
s1: 20 30
s1 is not equal to s2

main.cpp

#include <iostream>
#include <stdlib.h>
#include "stack.h"

using namespace std;

int main() 
{
   stackType<int> s1;
   stackType<int> s2;

   if (s1 == s2) 
   {
      cout << "empty stacks are equal" << endl;
   }
   s1.push(10); s1.push(20);
   s2.push(10);
   cout << "s1: ";
   cout << "s2: "; 
   cout << "s1 ";
   if (s1 != s2) 
   cout << "!";
   cout << "= s2" << endl;
   s2.push(20);
   cout << "s2: ";
   cout << "s1 ";
   if (s1 == s2) 
   {
      cout << "is equal ";
   } else {
      cout << "is not equal " << endl;
   }
   cout << "s2" << endl;
   s1.pop(); s1.pop();
   s1.push(30); s1.push(20);
   cout << "s1: ";
   cout << "s1 ";
   if (s1 != s2) 
   cout << " is";
   cout << " not equal to s2" << endl;

   system ("pause");
   return 0;
}

header file

#ifndef H_StackType
#define H_StackType

#include <iostream>
#include <cassert>

using namespace std;

template<class Type>

class stackType
{
      
      public:
             const stackType<Type>& operator=(const stackType<Type>&);
             void initializeStack();
             bool isEmptyStack();
             bool isFullStack();
             void destroyStack();
             void push(const Type& newItem);
             Type top();
             void pop();
             stackType(int stackSize = 100);
             stackType(const stackType<Type>& otherStack);
             ~stackType();
             
             
      private:
              int maxStackSize;
              int stackTop;
              Type *list;
              void copyStack(const stackType<Type>& otherStack);
};

template<class Type>
void stackType<Type>::copyStack(const stackType<Type>& otherStack)
{
     delete [] list;
     maxStackSize = otherStack.maxStackSize;
     stackTop = otherStack.stackTop;
     
     list = new Type[maxStackSize];
     assert(list != NULL);
     
     for(int j = 0; j < stackTop; j++)
     list[j] = otherStack.list[j];
     
}

template<class Type>
stackType<Type>::stackType(const stackType<Type>& otherStack)
{
      list = NULL;
      
      copyStack(otherStack);
      
}

template<class Type>
const stackType<Type>& stackType<Type>::operator=(const stackType<Type>& otherStack)
{
      if (this != &otherStack)
      copyStack(otherStack);
      
      return *this;
}

template<class Type>
void stackType<Type>::initializeStack()
{
     stackTop = 0;
}

template<class Type>
void stackType<Type>::destroyStack()
{
     stackTop = 0;
}

template<class Type>
bool stackType<Type>::isEmptyStack()
{
     return(stackTop == maxStackSize);
}

template<class Type>
bool stackType<Type>::isFullStack()
{
     return(stackTop == maxStackSize);
}

template<class Type>
void stackType<Type>::push(const Type& newItem)
{
     if(!isFullStack())
     {
         list[stackTop] = newItem;
         
         stackTop++;
     }
     
     else
         cerr<<"Cannot add to a full stack."<<endl;
}

template<class Type>
Type stackType<Type>::top()
{
     assert(stackTop != 0);
     
     return list[stackTop - 1];
}

template<class Type>
void stackType<Type>::pop()
{
     if(!isEmptyStack())
     {
        stackTop--;
     }
     else
        cerr<<"Cannot remove from an empty stack."<<endl;
}

template<class Type>
stackType<Type>::stackType(int stackSize)
{
     if(stackSize <= 0)
     {
       cerr<<"The size of the array to hold the stack must "
           <<"be positive."<<endl;
       cerr<<"Creating an array of size 100."<<endl;
   
       maxStackSize = 100;
     }
       
      else
        maxStackSize = stackSize;
        
        stackTop = 0;
        
        list = new Type[maxStackSize];
}

template<class Type>
stackType<Type>::~stackType()
{
     delete [] list;
}


#endif

Pretend you're the compiler. What does the author mean by "two objects are the same"? All their data members have the same value? Possibly, but there could be other definitions too. You'd have no idea, so you'd throw up your hands and quit. The compiler knows what == and != means for integers, strings, etc., but those aren't custom classes. Yours is. You need to write == and != operators.

Pop elements from both stacks and compare whether they are equal.

I am trying to create the functions for operator "==" and "!=". I am currently getting an error below: Any help is appreciated.


error:155 bool operator==(const stackType<Type>&)' must take exactly two arguments


But I dont really know what other arguement to take.

#ifndef H_StackType
#define H_StackType

#include <iostream>
#include <cassert>

using namespace std;

template<class Type>

class stackType
{
      
      public:
             const stackType<Type>& operator=(const stackType<Type>&);
             void initializeStack();
             bool isEmptyStack();
             bool isFullStack();
             void destroyStack();
             void push(const Type& newItem);
             Type top();
             void pop();
             stackType(int stackSize = 100);
             stackType(const stackType<Type>& otherStack);
             ~stackType();
             bool operator==(const stackType<Type>& otherStack);
              bool operator!=(const stackType<Type>& otherStack);
             
             
      private:
              int maxStackSize;
              int stackTop;
              Type *list;
              void copyStack(const stackType<Type>& otherStack);
         
};

template<class Type>
void stackType<Type>::copyStack(const stackType<Type>& otherStack)
{
     delete [] list;
     maxStackSize = otherStack.maxStackSize;
     stackTop = otherStack.stackTop;
     
     list = new Type[maxStackSize];
     assert(list != NULL);
     
     for(int j = 0; j < stackTop; j++)
     list[j] = otherStack.list[j];
     
}

template<class Type>
stackType<Type>::stackType(const stackType<Type>& otherStack)
{
      list = NULL;
      
      copyStack(otherStack);
      
}

template<class Type>
const stackType<Type>& stackType<Type>::operator=(const stackType<Type>& otherStack)
{
      if (this != &otherStack)
      copyStack(otherStack);
      
      return *this;
}

template<class Type>
void stackType<Type>::initializeStack()
{
     stackTop = 0;
}

template<class Type>
void stackType<Type>::destroyStack()
{
     stackTop = 0;
}

template<class Type>
bool stackType<Type>::isEmptyStack()
{
     return(stackTop == maxStackSize);
}

template<class Type>
bool stackType<Type>::isFullStack()
{
     return(stackTop == maxStackSize);
}

template<class Type>
void stackType<Type>::push(const Type& newItem)
{
     if(!isFullStack())
     {
         list[stackTop] = newItem;
         
         stackTop++;
     }
     
     else
         cerr<<"Cannot add to a full stack."<<endl;
}

template<class Type>
Type stackType<Type>::top()
{
     assert(stackTop != 0);
     
     return list[stackTop - 1];
}

template<class Type>
void stackType<Type>::pop()
{
     if(!isEmptyStack())
     {
        stackTop--;
     }
     else
        cerr<<"Cannot remove from an empty stack."<<endl;
}

template<class Type>
stackType<Type>::stackType(int stackSize)
{
     if(stackSize <= 0)
     {
       cerr<<"The size of the array to hold the stack must "
           <<"be positive."<<endl;
       cerr<<"Creating an array of size 100."<<endl;
   
       maxStackSize = 100;
     }
       
      else
        maxStackSize = stackSize;
        
        stackTop = 0;
        
        list = new Type[maxStackSize];
}

template<class Type>
stackType<Type>::~stackType()
{
     delete [] list;
}

template<class Type>
bool operator== (const stackType<Type>& otherStack)
{
bool eq=true;     
}

template<class Type>
bool operator!= (const stackType<Type>& otherStack)
{
     
 return !( this == otherStack );    

}


#endif

If you've already written an = operator that compiles, but your == and != operators don't compile, you should look at your = operator and see what you did correctly there that you did incorrectly with your == and != operators.

Thank you for replying Vernon Dozier.

Is it still necessary to use bool for operator== and operator!= or should i declare the function exactly the same as how I did operator=?

Thank you for replying Vernon Dozier.

Is it still necessary to use bool for operator== and operator!= or should i declare the function exactly the same as how I did operator=?

If it was exactly the same, it wouldn't make any sense and it would be unnecessary. Why have two binary operators that do exactly the same thing? == and != always return boolean values. The point is that you already wrote a binary operator that compiled successfully, so when you need to write another binary operator (==), use the one you already wrote as a skeleton and change as needed.

const stackType<Type>& stackType<Type>::operator=(const stackType<Type>& otherStack)

Now look at your == operator:

bool operator== (const stackType<Type>& otherStack)

See red on the first one and lack of red on the second one. How is the compiler to know that the == operator implementation has anything to do with the StackType class type unless you tell it, like you did with the = operator?

The reason I asked was because when I made the change:

bool stackType<Type>& operator==(bool stackType<Type>&);
bool stackType<Type>& operator!= (bool stackType<Type>&);


template<class Type>
bool stackType<Type>::operator==(bool stackType<Type>& otherStack)
{
bool eq=true;     
}

template<class Type>
bool stackType<Type>::operator!=(bool stackType<Type>& otherStack)
{
     
 return !(this == &otherStack);    

}

I received these errors in the header file:

26 `stackType<Type>' specified as declarator-id
26 perhaps you want `stackType<Type>' for a constructor
26 `bool' is now a keyword
26 expected `;' before '&' token
27 `stackType<Type>' specified as declarator-id
27 perhaps you want `stackType<Type>' for a constructor
27 `bool' is now a keyword
27 expected `;' before '&' token
155 expected unqualified-id before '&' token
155 expected `,' or `...' before '&' token
156 no `bool stackType<Type>::operator==(bool)' member function declared in class `stackType<Type>'
156 template definition of non-template `bool stackType<Type>::operator==(bool)'


I don't know if this has anything to do with the changes that I made or the contents that I have in my function so far.


header file:

#ifndef H_StackType
#define H_StackType

#include <iostream>
#include <cassert>

using namespace std;

template<class Type>

class stackType
{
      
      public:
             const stackType<Type>& operator=(const stackType<Type>&);
             void initializeStack();
             bool isEmptyStack();
             bool isFullStack();
             void destroyStack();
             void push(const Type& newItem);
             Type top();
             void pop();
             stackType(int stackSize = 100);
             stackType(const stackType<Type>& otherStack);
             ~stackType();
             bool stackType<Type>& operator==(bool stackType<Type>&);
             bool stackType<Type>& operator!= (bool stackType<Type>&);
             
             
      private:
              int maxStackSize;
              int stackTop;
              Type *list;
              void copyStack(const stackType<Type>& otherStack);

};

template<class Type>
void stackType<Type>::copyStack(const stackType<Type>& otherStack)
{
     delete [] list;
     maxStackSize = otherStack.maxStackSize;
     stackTop = otherStack.stackTop;
     
     list = new Type[maxStackSize];
     assert(list != NULL);
     
     for(int j = 0; j < stackTop; j++)
     list[j] = otherStack.list[j];
     
}

template<class Type>
stackType<Type>::stackType(const stackType<Type>& otherStack)
{
      list = NULL;
      
      copyStack(otherStack);
      
}

template<class Type>
const stackType<Type>& stackType<Type>::operator=(const stackType<Type>& otherStack)
{
      if (this != &otherStack)
      copyStack(otherStack);
      
      return *this;
}

template<class Type>
void stackType<Type>::initializeStack()
{
     stackTop = 0;
}

template<class Type>
void stackType<Type>::destroyStack()
{
     stackTop = 0;
}

template<class Type>
bool stackType<Type>::isEmptyStack()
{
     return(stackTop == maxStackSize);
}

template<class Type>
bool stackType<Type>::isFullStack()
{
     return(stackTop == maxStackSize);
}

template<class Type>
void stackType<Type>::push(const Type& newItem)
{
     if(!isFullStack())
     {
         list[stackTop] = newItem;
         
         stackTop++;
     }
     
     else
         cerr<<"Cannot add to a full stack."<<endl;
}

template<class Type>
Type stackType<Type>::top()
{
     assert(stackTop != 0);
     
     return list[stackTop - 1];
}

template<class Type>
void stackType<Type>::pop()
{
     if(!isEmptyStack())
     {
        stackTop--;
     }
     else
        cerr<<"Cannot remove from an empty stack."<<endl;
}

template<class Type>
stackType<Type>::stackType(int stackSize)
{
     if(stackSize <= 0)
     {
       cerr<<"The size of the array to hold the stack must "
           <<"be positive."<<endl;
       cerr<<"Creating an array of size 100."<<endl;
   
       maxStackSize = 100;
     }
       
      else
        maxStackSize = stackSize;
        
        stackTop = 0;
        
        list = new Type[maxStackSize];
}

template<class Type>
stackType<Type>::~stackType()
{
     delete [] list;
}

template<class Type>
bool stackType<Type>::operator==(bool stackType<Type>& otherStack)
{
bool eq=true;     
}

template<class Type>
bool stackType<Type>::operator!=(bool stackType<Type>& otherStack)
{
     
 return !(this == &otherStack);    

}


#endif

Go back to post 4. Change lines 154 - 166 to this:

template<class Type>
bool stackType<Type>::operator== (const stackType<Type>& otherStack)
{
   bool eq=true;
}

template<class Type>
bool stackType<Type>::operator!= (const stackType<Type>& otherStack)
{
   return !( this == otherStack );
}

All you want to do is add stackType<Type>:: before the word operator . Everything else stays the same except you do need to change the contents of your == function too since lines 156 - 158 don't do anything and you have no return statement. But your compiler problem goes away if you make the changes above.

[EDIT]
Well, they don't all go away necessarily, but the linker problems do. Just have == and != return true and false for now, get it to compile, then fix ==, then after that's done, worry about !=.
[/EDIT]

Thank you Vernon Dozier.

I was working on the program some more and corrected the issues and started populating my operator== and operator!= to try to get things working. However, I came across these 2 errors that I need some assistance on. Hope you can help!


Errors:

12 In member function `bool stackType<Type>::operator==(const stackType<Type>&) [with Type = int]': instantiated from here

158 passing `const stackType<int>' as `this' argument of `bool stackType<Type>::isFullStack() [with Type = int]' discards qualifiers

header file:

#ifndef H_StackType
#define H_StackType

#include <iostream>
#include <cassert>

using namespace std;

template<class Type>

class stackType
{
      
      public:
             const stackType<Type>& operator=(const stackType<Type>&);
             void initializeStack();
             bool isEmptyStack();
             bool isFullStack();
             void destroyStack();
             void push(const Type& newItem);
             Type top();
             void pop();
             stackType(int stackSize = 100);
             stackType(const stackType<Type>& otherStack);
             ~stackType();
             bool operator== (const stackType<Type>&);
             bool operator!= (const stackType<Type>&);
             
             
      private:
              int maxStackSize;
              int stackTop;
              Type *list;
              void copyStack(const stackType<Type>& otherStack);

};

template<class Type>
void stackType<Type>::copyStack(const stackType<Type>& otherStack)
{
     delete [] list;
     maxStackSize = otherStack.maxStackSize;
     stackTop = otherStack.stackTop;
     
     list = new Type[maxStackSize];
     assert(list != NULL);
     
     for(int j = 0; j < stackTop; j++)
     list[j] = otherStack.list[j];
     
}

template<class Type>
stackType<Type>::stackType(const stackType<Type>& otherStack)
{
      list = NULL;
      
      copyStack(otherStack);
      
}

template<class Type>
const stackType<Type>& stackType<Type>::operator=(const stackType<Type>& otherStack)
{
      if (this != &otherStack)
      copyStack(otherStack);
      
      return *this;
}

template<class Type>
void stackType<Type>::initializeStack()
{
     stackTop = 0;
}

template<class Type>
void stackType<Type>::destroyStack()
{
     stackTop = 0;
}

template<class Type>
bool stackType<Type>::isEmptyStack()
{
     return(stackTop == maxStackSize);
}

template<class Type>
bool stackType<Type>::isFullStack()
{
     return(stackTop == maxStackSize);
}

template<class Type>
void stackType<Type>::push(const Type& newItem)
{
     if(!isFullStack())
     {
         list[stackTop] = newItem;
         
         stackTop++;
     }
     
     else
         cerr<<"Cannot add to a full stack."<<endl;
}

template<class Type>
Type stackType<Type>::top()
{
     assert(stackTop != 0);
     
     return list[stackTop - 1];
}

template<class Type>
void stackType<Type>::pop()
{
     if(!isEmptyStack())
     {
        stackTop--;
     }
     else
        cerr<<"Cannot remove from an empty stack."<<endl;
}

template<class Type>
stackType<Type>::stackType(int stackSize)
{
     if(stackSize <= 0)
     {
       cerr<<"The size of the array to hold the stack must "
           <<"be positive."<<endl;
       cerr<<"Creating an array of size 100."<<endl;
   
       maxStackSize = 100;
     }
       
      else
        maxStackSize = stackSize;
        
        stackTop = 0;
        
        list = new Type[maxStackSize];
}

template<class Type>
stackType<Type>::~stackType()
{
     delete [] list;
}

template<class Type>
bool stackType<Type>::operator== (const stackType<Type>& otherStack)
{

if (otherStack.isFullStack() != isFullStack()) 

cerr<<"They are not the same size"<<endl;

for(int j = 0 ; j < isFullStack(); ++j)
{
if (otherStack.list[j] != list[j])

return false;

else

return true;
}

}

template<class Type>
bool stackType<Type>::operator!= (const stackType<Type>& otherStack)
{
     
return !(*this == otherStack);

}



#endif

main.cpp

#include <iostream>
#include <stdlib.h>
#include "stack.h"

using namespace std;

int main() 
{
   stackType<int> s1;
   stackType<int> s2;

   if (s1 == s2) 
   {
      cout << "empty stacks are equal" << endl;
   }
   s1.push(10); s1.push(20);
   s2.push(10);
   cout << "s1: ";
   cout << "s2: "; 
   cout << "s1 ";
   if (s1 != s2) 
   cout << "!";
   cout << "= s2" << endl;
   s2.push(20);
   cout << "s2: ";
   cout << "s1 ";
   if (s1 == s2) 
   {
      cout << "is equal ";
   } else {
      cout << "is not equal " << endl;
   }
   cout << "s2" << endl;
   s1.pop(); s1.pop();
   s1.push(30); s1.push(20);
   cout << "s1: ";
   cout << "s1 ";
   if (s1 != s2) 
   cout << " is";
   cout << " not equal to s2" << endl;

   system ("pause");
   return 0;
}

You need to read these error messages carefully. They tell you exactly what's wrong.

158 passing `const stackType<int>' as `this' argument of `bool stackType<Type>::isFullStack() [with Type = int]' discards qualifiers

The word "qualifier" should pop out at you. Compare the functions. One has "const", one doesn't. "const" is a qualifier. There's a decent chance that you are using the "const" qualifier incorrectly. To find out, go through the file and replace "const" with "/*const*/" everywhere and recompile. if it compiles correctly this time, you know what the problem is for sure, so if you don't yet know how to use "const", put the project aside, learn how, and come back to it. Then fix the "const" problem. Then change the "/*const*/" back to "const" and go from there. The error messages tell you exactly what's wrong. Read them carefully.

Alright I will try to see if I can figure it out. Does everything else look fine in my operator== and operator!=? or does it still needs work. Thanks

Alright I will try to see if I can figure it out. Does everything else look fine in my operator== and operator!=? or does it still needs work. Thanks

== still needs work. You'll need to rewrite it even after you fix the "const" problem(s).

Hey, I finally figured it out even though it took several hours. However, I have one last question. In main.cpp, how do I print out each stack so that it looks like the Expected output below. Thanks!

Current output:

s1:
s2:
s1 != s2
s2:
s1 is equal s2
s1:
s1 is not equal to s2


Expected output:

s1: 20 10
s2: 10
s1 != s2
s2: 20 10
s1 is equal s2
s1: 20 30
s1 is not equal to s2

main.cpp


#include <iostream>
#include <stdlib.h>
#include "stack.h"

using namespace std;

int main() 
{
stackType<int> s1;
stackType<int> s2;

if (s1 == s2) 
{
cout << "empty stacks are equal" << endl;
}
s1.push(10); s1.push(20);
s2.push(10);
cout << "s1: "<<endl;
cout << "s2: "<<endl; 
cout << "s1 ";
if (s1 != s2) 
cout << "!";
cout << "= s2" << endl;
s2.push(20);
cout << "s2: "<<endl;
cout << "s1 ";
if (s1 == s2) 
{
cout << "is equal ";
} else {
cout << "is not equal " << endl;
}
cout << "s2" << endl;
s1.pop(); s1.pop();
s1.push(30); s1.push(20);
cout << "s1: "<<endl;
cout << "s1 ";
if (s1 != s2) 
cout << "is";
cout << " not equal to s2" << endl;

system ("pause");
return 0;
}

header file:

#ifndef H_StackType
#define H_StackType

#include <iostream>
#include <cassert>

using namespace std;

template<class Type>

class stackType
{
      
      public:
             const stackType<Type>& operator=(const stackType<Type>&);
             void initializeStack();
             bool isEmptyStack();
             bool isFullStack();
             bool operator==(const Stack<T> &s1)
             bool operator!=(const Stack<T> &s1)
             void destroyStack();
             void push(const Type& newItem);
             Type top();
             void pop();
             stackType(int stackSize = 100);
             stackType(const stackType<Type>& otherStack);
             ~stackType();
             
             
      private:
              int maxStackSize;
              int stackTop;
              Type *list;
              void copyStack(const stackType<Type>& otherStack);
};

template<class Type>
void stackType<Type>::copyStack(const stackType<Type>& otherStack)
{
     delete [] list;
     maxStackSize = otherStack.maxStackSize;
     stackTop = otherStack.stackTop;
     
     list = new Type[maxStackSize];
     assert(list != NULL);
     
     for(int j = 0; j < stackTop; j++)
     list[j] = otherStack.list[j];
     
}

template<class Type>
stackType<Type>::stackType(const stackType<Type>& otherStack)
{
      list = NULL;
      
      copyStack(otherStack);
      
}

template<class Type>
const stackType<Type>& stackType<Type>::operator=(const stackType<Type>& otherStack)
{
      if (this != &otherStack)
      copyStack(otherStack);
      
      return *this;
}

template<class Type>
void stackType<Type>::initializeStack()
{
     stackTop = 0;
}

template<class Type>
void stackType<Type>::destroyStack()
{
     stackTop = 0;
}

template<class Type>
bool stackType<Type>::isEmptyStack()
{
     return(stackTop == maxStackSize);
}

template<class Type>
bool stackType<Type>::isFullStack()
{
     return(stackTop == maxStackSize);
}

template<class Type>
void stackType<Type>::push(const Type& newItem)
{
     if(!isFullStack())
     {
         list[stackTop] = newItem;
         
         stackTop++;
     }
     
     else
         cerr<<"Cannot add to a full stack."<<endl;
}

template<class Type>
Type stackType<Type>::top()
{
     assert(stackTop != 0);
     
     return list[stackTop - 1];
}

template<class Type>
void stackType<Type>::pop()
{
     if(!isEmptyStack())
     {
        stackTop--;
     }
     else
        cerr<<"Cannot remove from an empty stack."<<endl;
}

template<class Type>
stackType<Type>::stackType(int stackSize)
{
     if(stackSize <= 0)
     {
       cerr<<"The size of the array to hold the stack must "
           <<"be positive."<<endl;
       cerr<<"Creating an array of size 100."<<endl;
   
       maxStackSize = 100;
     }
       
      else
        maxStackSize = stackSize;
        
        stackTop = 0;
        
        list = new Type[maxStackSize];
}

template<class Type>
stackType<Type>::~stackType()
{
     delete [] list;
}


    bool operator==(const Stack<T> &s1) {
        bool eq = true;

        if (s1.stackLen() != this->stackLen()) {
            eq = false;
        } else if (s1.empty() == false) {
            size_t len = this->stackLen();
            T *p = this->_top;
            T *q = s1._top;
            for (size_t i = 0; (i < len) && (eq == true); i++) {
                eq = (*p++ == *q++);
            }
        }
        return eq;
    }
    bool operator!=(const Stack<T> &s1) {
        return !(*this == s1);
    }
    void push(T n) {
        if (full() == true) grow();
        *(--_top) = n;
    }
    T pop() {
        T x = *_top;
        if (empty() == false) _top++;
    &n


#endif

Did you even bother to compile this before posting it? Even a cursory look at it makes it obvious that it won't compile. Look at line 179 for instance. There is no "actual output" because this doesn't even come close to compiling or running. Proofread before you post. There's an "edit" button if you accidentally copy and paste the wrong thing or whatever. I can't imagine what else this post could have been.

I apologize. I corrected the information below. I do not know how that other stuff got in that post. Right now, everything compiles and it looks fine except that I do not know how to print out the information/numbers that are contained in stacks: s1 and s2.

Current output:

s1:
s2:
s1 is not equal to s2
s2:
s1 is equal s2
s1:
s1 is not equal to s2

Expected output:

s1: 20 10
s2: 10
s1 is not equal to s2
s2: 20 10
s1 is equal s2
s1: 20 30
s1 is not equal to s2

main.cpp

#include <iostream>
#include <stdlib.h>
#include "stack.h"

using namespace std;

int main() 
{
   stackType<int> s1;
   stackType<int> s2;


if (s1 == s2) 
{
cout << "empty stacks are equal" << endl;
}
s1.push(10); s1.push(20);
s2.push(10);
cout << "s1: "<<endl;
cout << "s2: "<<endl;

if (s1 != s2)
cout << "s1 is not equal to s2" << endl;
s2.push(20);
cout << "s2: "<<endl;


if (s1 == s2) 
{
cout << "s1 is equal to ";
} 

else 
{
cout << "s1 is not equal to  " << endl;
}
cout << "s2" << endl;
s1.pop(); s1.pop();
s1.push(30); s1.push(20);
cout << "s1: "<<endl;

if (s1 != s2)
cout << "s1 is not equal to s2" << endl;

system("PAUSE");
return 0;
}

header file:

#ifndef H_StackType
#define H_StackType

#include <iostream>
#include <cassert>

using namespace std;

template<class Type>

class stackType
{
      
      public:
             const stackType<Type>& operator=(const stackType<Type>&);
             void initializeStack();
             bool isEmptyStack();
             bool isFullStack();
             void destroyStack();
             void push(const Type& newItem);
             Type top();
             void pop();
             stackType(int stackSize = 100);
             stackType(const stackType<Type>& otherStack);
             ~stackType();
             bool operator== (const stackType<Type>&);
             bool operator!= (const stackType<Type>&);
             
             
             
             
      private:
              int maxStackSize;
              int stackTop;
              Type *list;
              void copyStack(const stackType<Type>& otherStack);
              bool isEqual(const stackType<Type>&);

};

template<class Type>
void stackType<Type>::copyStack(const stackType<Type>& otherStack)
{
     delete [] list;
     maxStackSize = otherStack.maxStackSize;
     stackTop = otherStack.stackTop;
     
     list = new Type[maxStackSize];
     assert(list != NULL);
     
     for(int j = 0; j < stackTop; j++)
     list[j] = otherStack.list[j];
     
}

template<class Type>
stackType<Type>::stackType(const stackType<Type>& otherStack)
{
      list = NULL;
      
      copyStack(otherStack);
      
}

template<class Type>
const stackType<Type>& stackType<Type>::operator=(const stackType<Type>& otherStack)
{
      if (this != &otherStack)
      copyStack(otherStack);
      
      return *this;
}

template<class Type>
void stackType<Type>::initializeStack()
{
     stackTop = 0;
}

template<class Type>
void stackType<Type>::destroyStack()
{
     stackTop = 0;
}

template<class Type>
bool stackType<Type>::isEmptyStack()
{
     return(stackTop == maxStackSize);
}

template<class Type>
bool stackType<Type>::isFullStack()
{
     return(stackTop == maxStackSize);
}

template<class Type>
void stackType<Type>::push(const Type& newItem)
{
     if(!isFullStack())
     {
         list[stackTop] = newItem;
         
         stackTop++;
     }
     
     else
         cerr<<"Cannot add to a full stack."<<endl;
}

template<class Type>
Type stackType<Type>::top()
{
     assert(stackTop != 0);
     
     return list[stackTop - 1];
}

template<class Type>
void stackType<Type>::pop()
{
     if(!isEmptyStack())
     {
        stackTop--;
     }
     else
        cerr<<"Cannot remove from an empty stack."<<endl;
}

template<class Type>
stackType<Type>::stackType(int stackSize)
{
     if(stackSize <= 0)
     {
       cerr<<"The size of the array to hold the stack must "
           <<"be positive."<<endl;
       cerr<<"Creating an array of size 100."<<endl;
   
       maxStackSize = 100;
     }
       
      else
        maxStackSize = stackSize;
        
        stackTop = 0;
        
        list = new Type[maxStackSize];
}

template<class Type>
stackType<Type>::~stackType()
{
     delete [] list;
}



template<class Type>
bool stackType<Type>::isEqual(const stackType<Type>& otherStack) {
bool bRet = false;
if (otherStack.maxStackSize == maxStackSize && otherStack.stackTop == stackTop){
bRet = true;
for (int j = 0; j < stackTop; ++j) {
if (otherStack.list[j] != list[j]) {
// cout << "!=( " << j << ") " << otherStack.list[j] << "!=" << list[j];
bRet = false;
break;
}
}
}
return bRet;
}

template<class Type>
bool stackType<Type>::operator==(const stackType<Type>& otherStack) {
return isEqual(otherStack);
}

template<class Type>
bool stackType<Type>::operator!=(const stackType<Type>& otherStack) {
return !isEqual(otherStack); //!(*this == otherStack);
}


#endif

Write a << operator. In line 52, you use the [] operator, so you have the ability to isolate the individual elements of list. Loop through the elements in list, isolate an element, then display that individual list element using its own << operator (one that has nothing to do with stackType's << operator. int, string, double, etc., all have their own << operators already).

In the future, write the << operator early, long before the == and != operators. How does one debug if he/she cannot display an object's contents?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.