我是 c++ 的新手,我试图使用 c++ 实现出色的文件处理。 在下面的代码中,我从基类异常中继承了类Divide_By_Zero_Exception。运行代码后出现错误
错误:"{"标记之前的预期类名 {
如果我公开继承类名,为什么我需要指定类名。如果我需要指定它如何做。
//program to throw an exception if denominator is zero
#include <iostream>
#include <exception>
class Divide_By_Zero_Exception : public exception
{
public:
const char * what() const throw() {
return "Divide By Zero Exception";
}
};
using namespace std;
int main()
{
Divide_By_Zero_Exception d;
int n1, n2;
cin >> n1 >> n2;
try
{
if (n2 == 0)
throw d;
else
cout << n1 / n2;
}
catch (exception& e) {
cout << e.what();
}
return 0;
}
它std::exception
不exception
,因为当您使用该名称时,您的using
-指令尚未生效。
此外,而不是
const char * what() const throw()
你可能想写
const char * what() const noexcept