小编use*_*424的帖子

捕获异常:除以零

当我尝试除以0时,下面的代码没有捕获异常.我是否需要抛出异常,或者计算机是否在运行时自动抛出一个异常?

int i = 0;

cin >> i;  // what if someone enters zero?

try {
    i = 5/i;
}
catch (std::logic_error e) {

    cerr << e.what();
}
Run Code Online (Sandbox Code Playgroud)

c++ exception-handling

35
推荐指数
3
解决办法
8万
查看次数

重载<< operator ostream

为什么下面的线不起作用?

#include <iostream>

std::ostream& operator <<( std::ostream& os, const char *c)
{
    os << c; // why does this line doesn't work?
    return os;
}

int main()
{
    const char *c = "Hi";
    std::cout << c;
}
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading

3
推荐指数
1
解决办法
414
查看次数