наследование класса исключений c ++ для временного класса - PullRequest
0 голосов
/ 11 мая 2018
#include <iostream>
#include <string>

using namespace std;


class StackEmptyException {

private:

string errorMsg;

public:

StackEmptyException(const string& err) { errorMsg = err; }
string getMessage() const { return errorMsg; }

};



template <typename E>
class LinkedStack;

template <typename E>
template 
class Node {

private:

E element;
Node* next;

public:

Node(const E& e, Node* n) { element = e; next = n; }

friend class LinkedStack<E>;

};





template <typename E>
class LinkedStack {

private:

Node* tp;

public:

LinkedStack();
~LinkedStack();
LinkedStack(const LinkedStack& ls);
LinkedStack& operator=(const LinkedStack& ls);
int size() const;
bool isEmpty() const;
void push(const E& e);
void pop() throw(StackEmptyException);
const E& top() throw(StackEmptyException);

};





template <typename E>
LinkedStack::LinkedStack() {
tp = NULL;
}



template <typename E>
LinkedStack::LinkedStack(const LinkedStack& ls) {
tp = NULL;
Node* p = ls.tp;
Node* prev = NULL;

while (p != NULL) {
Node* v = new Node(p->element, NULL);
if (tp == NULL) tp = v;
else prev->next = v;
prev = v;
p = p->next;
}
}



template 
bool LinkedStack::isEmpty() const { return size() == 0; }



template <typename E>
LinkedStack& LinkedStack::operator=(const LinkedStack& ls) {
if (this != &ls) {
while (!isEmpty()) pop();
tp = NULL;
Node* p = ls.tp;
Node* prev = NULL;

while (p != NULL) {
Node* v = new Node(p->element, NULL);
if (tp == NULL) tp = v;
else prev->next = v;
prev = v;
p = p->next;
}
}

return *this;

}



template <typename E>
int LinkedStack::size() const {

int count = 0;

Node* v = tp;

while (v != NULL) {
v = v->next;
count++;
}

return count;

}





template <typename E>
void LinkedStack::push(const E& e) {

Node* v = new Node(e, tp);

tp = v;

}





template <typename E>
void LinkedStack::pop() throw(StackEmptyException) {

if (size() == 0) throw("Stack is Empty");

Node* old = tp;

tp = tp->next;

delete old;

}



template <typename E>
const E& LinkedStack::top() throw(StackEmptyException) {

if (size()==0) throw("Stack is Empty");

return tp->element;

}





template <typename E>
LinkedStack::~LinkedStack() {

while (!isEmpty()) pop();

}





int main() {

int a[5] = { 1, 2, 3, 4, 5 };

LinkedStack s;
for (int i = 0; i<5; i++) s.push(a[i]);
cout << "size = " << s.size() << " " << endl;

LinkedStack rS(s);
for (int i = 0; i<3; i++) rS.pop();

s = rS;
cout << "Current top's element = " << s.top() << endl; 

try {
for (int i = 0; i < 3; i++) s.pop();
}
catch(StackEmptyException e){
cout << e.getMessage() << endl;
}

cout << "size = " << s.size() << " " << endl;
}

Я хочу, за исключением того, что с помощью 'StackEmptyException class' но мой код не работает ... когда я использую этот метод в не шаблонном коде (разделенном на header, cpp, main), он работает!

В любом случае, если я изменю раздел try-catch на => for (int i = 0; i <2; i ++), тогда он будет работать хорошо.но «более 3» не работает. </p>

в чем проблема.

1 Ответ

0 голосов
/ 11 мая 2018

Вы выбросили неправильный тип исключения, оно должно быть

template <typename E>
void LinkedStack::pop() throw(StackEmptyException) {
    if (size() == 0) throw StackEmptyException("Stack is Empty");
    Node* old = tp;
    tp = tp->next;
    delete old;
}

Спецификация Throw не помогает компилятору создавать правильный тип вопреки возвращаемому типу. (У вас может быть несколько спецификаций броска).

...