c++ - c++ exeption 类继承到模板类
问题描述
#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'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;
}
抛出规范无助于编译器构造与返回类型相反的正确类型。(您可能有几个 throw 规范)。
推荐阅读
- javascript - 通过 WebRTC 发送时,video.captureStream 变为绿色
- javascript - 制作新频道 discord.js v11.3
- python - 保存文件前发送电子邮件
- nginx - nginx 服务器块路由与 cname 组合
- ios - iOS日历中如何使用通用链接打开添加事件的提示
- assembly - 在 Pep/9 汇编代码中调用 STRO 时出现看似随机的“1”
- r - 将字符串传递给 R 中列子集的名称
- android - 如何使用 Volley Android Java 将 json 数据插入服务器?
- rest - 使用光纤发送空响应
- azure-devops - 在 Microsoft Teams 上添加与 Teams 使用的浏览器相关的 Azure DevOps 连接器时出错