1

In our assignment it was given to us a stack class, this is the stack.h :

#ifndef STACK_H
#define STACK_H
#include "MyException.h"
#include <iostream>
using namespace std;

template<class T>
class Stack;

template<class T>
ostream& operator<<(ostream&,Stack<T>&);

template<class T>
class Stack
{
public:

    /*The constructor for the Stack class*/
    Stack();

            /*The overloaded assignment operator.  You will have to replace this 
    operator with an appropriate equivalent in your Java code*/

    Stack<T>& operator=(const Stack<T>& other);

    private:
    /*The node class.*/
    class Node
    {
        public:
            Node(const T& data, Node* n = 0)
            {
                element = data;
                next = n;
            }

            T element;
            Node* next;
    };

    /*The top of the stack*/
    Node* top;  
   };
   #include "Stack.C"
   #endif

And they gave us a Queue class, and this is the queue.h:

#ifndef QUEUE_H
#define QUEUE_H
#include <iostream>
#include "MyException.h"
#include "Stack.h"

using namespace std;

template<class T>
class Queue;
/*The Queue class should be implemented in terms a of a Stack object.
Complete the Stack class first before attempting to implement this class.*/

template<class T>
class Queue
{
public:
    /*The default constructor*/
    Queue();

    /*The copy constructor*/
    Queue(const Queue<T>& other);
    private:
    /*You must store your elements in this stack*/
    Stack<T>* stack;
};
    #include "Queue.C"
    #endif

So my problem is that I have implemented correctly the copy constructor in Stack.C but im not sure about how it should be done in Queue.C. I tried something but it gives me Segmentation fault. This is what I tried:

template<class T>
Queue<T>::Queue(const Queue<T>& other)
{
Stack<T> *test = new Stack<T>(*other.stack);
 }

Can anyonw please help me solve this? Thank you

beckinho
  • 33
  • 11
  • 2
    FYI, you don't have a stack copy constructor. You have overloaded the `=` operator. – 001 Feb 10 '14 at 14:19
  • I would look at this: http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three – drescherjm Feb 10 '14 at 14:22
  • 1
    Please tell me they're *not* teaching your to write template implementation in a separate `.C` file and then `#include`-ing it as you are here. And to the actual question, your stack object doesn't have proper copy semantics yet, so it'll be awhile before your Queue has them. Start from the bottom up. – WhozCraig Feb 10 '14 at 15:00
  • @JohnnyMopp I made a mistake when I copied, but they also ask us to implement the assignment operator – beckinho Feb 10 '14 at 15:19
  • @beckinho Edit the post to include your implementation of the copy constructor from the Stack class so we do not have to guess. – drescherjm Feb 10 '14 at 16:46

1 Answers1

2

Assuming your Stack copy constructor is actually correct, it looks like it could just be a null pointer issue. Currently, you're not checking that other.stack is valid before you try to dereference it. You should do something like this instead:

Stack<T> *test = 0;
if (other.stack != 0)
    test = new Stack<T>(*other.stack);

It seems strange that you're assigning to a local pointer in a copy constructor though. The result of new should probably be getting assigned to the member pointer (i.e. stack).

As a side note, there are some rather bad issues/practices in the rest of the code (such as the lack of destructors, the using namespace directive in a header file, and the source file includes). If this is code you've been given by an instructor/teacher, then be very careful about what you learn from them. It doesn't look like they entirely know what they're doing.

Peter Bloomfield
  • 5,578
  • 26
  • 37
  • Thanks alot for your time. They do ask us to implement other methods like destructor and enqueue() and dequeue(). When I test my code for all of them, verything works perfectly, its just when I test the copy constructor it gives me segmentation fault, even with your code. Any ideia what else might be wrong? – beckinho Feb 10 '14 at 15:22
  • 1
    @beckinho : As far as I can see, there's nothing else in the code you've shown that can cause a segfault on its own. The problem must be somewhere else (e.g. the `Stack` copy constructor). – Peter Bloomfield Feb 10 '14 at 15:57