Assignment Operator Trouble in Stack Class

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#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!
Should I include my code for Array as well? I think instead, there must be something simple I'm missing here.
Last edited on
Bug was on line 36 in the source code:

this->Stack<Type>::operator = (source);
Topic archived. No new replies allowed.