#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 类”来排除 buf 我的代码不工作... 当我在非模板代码(分为标题、cpp、main)中使用此方法时, 它正在工作!
无论如何, 如果我将 try-catch 部分修改为 => for (int i = 0; i < 2; i++) 然后它运作良好。 但“超过 3”不起作用。
有什么问题。
最佳答案
你抛出错误类型的异常,应该是
template <typename E>
void LinkedStack::pop() throw(StackEmptyException) {
if (size() == 0) throw StackEmptyException("Stack is Empty");
Node* old = tp;
tp = tp->next;
delete old;
}
抛出规范无助于编译器构造与返回类型相反的正确类型。 (您可能有多个抛出规范)。
关于c++ 异常类继承到模板类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50284578/