我目前正在学习异常处理,这是我的代码的一部分:
//vector.cpp
#include "vector.h"
Vector::Vector(int s)
{
if (s < 0) throw length_error{};
elem = new double[s];
sz = s;
}
这是我试图通过以下方式测试异常的代码:
try{
Vector v(-27);
}
catch (length_error)
{
cout << "There was a negative size for your vector.\n";
}
catch (bad_alloc)
{
cout << "The memory was not allocated properly.\n";
}
当我尝试运行我的应用程序时,我收到以下错误:
error C2440: '<function-style-cast>' : cannot convert from 'initializer-list' to 'std::length_error'
错误在哪里?