I’m creating a new Stack class for data storage, which uses my Array class as a data member. I’m still setting up constructors and having trouble with the assignment operator.
When I call my assignment operator, it is called continuously until I manually cancel the program.
Could you please help me find the mistake?
I can provide code for the Array class if necessary, but I think the bug should contained somewhere below.
Relevant code is as follows:
Stack Header Code:
#ifndef STACK_HPP
#define STACK_HPP
#include "Array_H.hpp"
namespace CLARK{
namespace Containers{
template <class Type=T> class Stack
{
private:
int m_current;
Array<Type> m_array;
public:
// constructors and destructors:
Stack(); // default constructor
// ...
~Stack(); // destructor
// ...
// modifiers:
// overloaded operator functions:
Stack<Type>& operator = (const Stack<Type>& source); // assignment operator
};
}
}
#ifndef STACK_CPP
#include "Stack.cpp"
#endif
#endif
Stack Source Code:
#ifndef STACK_CPP
#define STACK_CPP
#include "Stack_H.hpp"
namespace CLARK{
namespace Containers{
// constructors and destructors:
template <class Type>
Stack<Type>::Stack() : m_array(Array<Type>()) , m_current(0)
{ // default constructor
cout << "Stack constructor call (default)" << endl;
}
// ...
template <class Type>
Stack<Type>::~Stack()
{ // destructor
cout << "Stack destructor call" << endl;
}
// ...
// modifiers:
// overloaded operator functions:
template <class Type>
Stack<Type>& Stack<Type>::operator = (const Stack<Type>& source)
{// assignment operator
cout << "Stack assignment operator call" << endl;
if (this == &source)
return *this;
this->Stack<Type>::operator = (source);
m_current = source.m_current;
m_array = source.m_array;
return *this;
}
}
}
#endif STACK_CPP
Test Code:
#include "Point_H.hpp"
#include "Line_H.hpp"
#include "Circle_H.hpp"
#include "Array_H.hpp"
#include "NumericArray_H.hpp"
#include "Stack_H.hpp"
#include "PointArray_H.hpp"
#include "ArrayException_H.hpp"
#include "OutOfBoundsException_H.hpp"
using namespace CLARK::Containers;
using namespace CLARK::CAD;
int main()
{
try
{
Stack<int> testStack; // test default constructor
Stack<int> testStack2;
testStack2 = testStack; // test assignment operator
return 0;
} catch(ArrayException& err) {
cout << err.GetMessage() << endl;
}
}
The output looks like:
Array constructor call (default)
Stack constructor call (default)
Array constructor call
Stack constructor call
Array constructor call (default)
Stack constructor call (default)
Stack assignment operator call
Stack assignment operator call
Stack assignment operator call
Stack assignment operator call
Stack assignment operator call
Stack assignment operator call
Stack assignment operator call [repeats infinitely, until I cancel it]
Thanks.
The call to
Stack<T>::operator=(source)calls the operator you are implementing. So you end up with an infinite recusion. It looks a bit as if you wanted to call the assignment operator of a base class but yourStackclass template doesn’t have a base class.Since I noticed that you checked for self-assignment, please note that assignment operators doing a self-assignment check either make an unnecessary check or are not exception-safe.