我有这样的模板标题:
template<class TypeA, size_t tsize=100, class Exc=std::out_of_range>
和一个抛出 Exc 类型异常的添加函数:
void add(TypeA* objA) {
if(nelems==capac) {
delete objA;
throw Exc e; //the line in question
}
nelems++;
elems[nelems-1]=objA;
}
我有以下错误信息:
error: expected primary-expression before ‘e’
throw Exc e;
我做错了什么?
最佳答案
根据 this page , 表达式 throw
需要另一个表达式。
但您提供的throw
表达式带有声明
,这不是表达式
。
尝试
Exc e{"message"};
throw e;
或
throw Exc{"message"};
正如贾斯汀对你的问题所建议的那样。
关于c++ - 如何抛出 "template parameter"类型的异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50032866/