捕获模板中的异常类
Catching an exception class within a template
本文关键字:异常 更新时间:2023-10-16
我在为正在创建的Stack模板使用异常类Overflow((时遇到问题。如果我定期定义类,就没有问题。如果我将类定义为模板,我就无法使对catch((的调用正常工作。我有一种感觉,这只是语法,但我一辈子都搞不清楚
#include<iostream>
#include<exception>
using namespace std;
template <class T>
class Stack
{
private:
T *stackArray;
int size;
int top;
public:
Stack(int size) { this->size = size; stackArray = new T[size]; top = 0; }
~Stack() { delete[] stackArray; }
void push(T value)
{
if (isFull())
throw Overflow();
stackArray[top] = value;
top++;
}
bool isFull()
{
if (top == size)
return true;
else
return false;
}
class Overflow {};
};
int main()
{
try
{
Stack<double> Stack(5);
Stack.push( 5.0);
Stack.push(10.1);
Stack.push(15.2);
Stack.push(20.3);
Stack.push(25.4);
Stack.push(30.5);
}
catch (Stack::Overflow)
{
cout << "ERROR! The stack is full.n";
}
return 0;
}
问题出在catch(Stack::Overflow(语句中。正如我所说,如果这个类不是一个模板,那么它就可以正常工作。然而,一旦我将其定义为模板,它就不再起作用了。我尝试过各种语法,但总是从编译器中得到两组错误消息中的一组。
如果我使用catch(Stack::Overflow(:
ch18pr01.cpp(89) : error C2955: 'Stack' : use of class template requires template argument list
ch18pr01.cpp(13) : see declaration of 'Stack'
ch18pr01.cpp(89) : error C2955: 'Stack' : use of class template requires template argument list
ch18pr01.cpp(13) : see declaration of 'Stack'
ch18pr01.cpp(89) : error C2316: 'Stack<T>::Overflow' : cannot be caught as the destructor and/or copy constructor are inaccessible
编辑:我的意思是
如果我使用catch(Stack<double>:溢出(或其任何变体:
ch18pr01.cpp(89) : error C2061: syntax error : identifier 'Stack'
ch18pr01.cpp(89) : error C2310: catch handlers must specify one type
ch18pr01.cpp(95) : error C2317: 'try' block starting on line '75' has no catch handlers
我实在想不通。有人知道吗?
重点是你应该放
class overflow;
外部
class Stack<>
因为这是一个一般的例外,而不是指定类型。
catch (const Stack<double>::Overflow & obj)
//^^^^^^^^ note this!
也就是说,您还必须提供类型。
还要注意,我接受该对象作为const
引用,以避免复制原始Overflow
对象!
顺便问一下,为什么要将Overflow
作为嵌套类型?你有什么理由?我看不出有什么令人信服的理由这么做。如果你在Stack
类模板之外定义它会更好。
您必须指定正在使用的模板实例化:
try
{
Stack<double> Stack(5);
Stack.push( 5.0);
Stack.push(10.1);
Stack.push(15.2);
Stack.push(20.3);
Stack.push(25.4);
Stack.push(30.5);
}
catch (Stack<double>::Overflow)
{
cout << "ERROR! The stack is full.n";
}
如果不指定模板参数,编译器会感到困惑,因为它不知道要使用哪个类(例如Stack<int>
或Stack<double>
?(。
附带说明:尽量避免对类型和变量使用相同的名称(本例中为Stack
和Stack
(,这会使可读性和错误跟踪更加困难。
相关文章:
- 处理多个异常集合的C++方法
- 我在c++代码中生成了一个运行时#3异常
- 孤立代码块在结构中引发异常
- C++中的赋值发生,尽管右侧出现异常
- 从构造函数抛出异常时如何克服内存泄漏
- 异常属于C++中的线程还是进程
- 当类定义不可见时捕获异常
- 引发异常:读取访问冲突**dynamicArray**为0x1118235.发生
- 为什么异常不退出程序?
- 为什么我应该在异常处理中使用std::cerr而不是std::cout
- 如何修复链表类实现的未处理异常0xDDDDDDDD
- 关于:C++中异常对象的范围:为什么我没有得到副本?
- 是什么导致了Unity 3D中的"错误线程异常"?
- 如何将strftime中的格式错误作为异常捕获
- 创建具有 new in 函数和"this is nullptr"异常的对象
- 尝试使用智能指针时引发异常
- 函数如何通知用户它基于函数原型抛出异常?
- 是否值得降低我的代码的可读性,以便在出现内存不足错误时提供异常安全性?
- 当我使用 C++ 中的 C# dll 来使用 Selenium 时,存在异常处理问题
- Pytorch torch.cholesky忽略异常