Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8605251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:46:55+00:00 2026-06-12T02:46:55+00:00

I’m creating a new Stack class for data storage, which uses my Array class

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:46:57+00:00Added an answer on June 12, 2026 at 2:46 am

    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 your Stack class 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have an array which has BIG numbers and small numbers in it. I
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.