В нашем назначении он был дан нам класс стека, это 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
И они дали нам класс Queue, и это 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
Так что моя проблема в том, что я реализовал правильно конструктор копирования в Stack.C, но им не уверен, о том, как это должно быть сделано в Queue.C. Я что-то пробовал, но это дает мне ошибку сегментации. Это то, что я пробовал:
template<class T>
Queue<T>::Queue(const Queue<T>& other)
{
Stack<T> *test = new Stack<T>(*other.stack);
}
Может ли кто-нибудь помочь мне решить эту проблему? Спасибо
FYI, у вас нет конструктора копии стека. Вы перегрузили оператор '='. –
Я бы посмотрел на это: http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three – drescherjm
Пожалуйста, скажите мне, что они не учат писать реализацию шаблона в отдельном ' .C', а затем '# include'-ing, как вы здесь. И к фактическому вопросу, у вашего объекта стека еще нет правильной семантики семантики, так что это будет некоторое время, прежде чем ваша очередь будет иметь их. Начните снизу вверх. – WhozCraig