c++ - 如何通过包含要打印的字符串的类函数打印异常?

标签 c++ exception cout

我有这个:

catch (Except& e) {
  std::cout << e.print() << std::endl;
}

我想要这样打印:好的,你赢了!

所以我上课了:

class Except{
public:
  std::string print() {
    std::string error("OK you won!\n");
    return error;
  }
};

我在 Except 类中有这个错误:“ namespace ‘std’中的‘string’没有命名类型”

最佳答案

您必须包含 std::string 的标题: #include <string>

关于c++ - 如何通过包含要打印的字符串的类函数打印异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21098361/

相关文章:

C++ : automatic const?

c++ - 你如何在 C++ 中打印对象的地址?

c++ - 使用 Runge-Kutta(二阶)积分波动方程

c++ - 构造函数中虚函数的奇怪行为

java.rmi.ServerException : RemoteException occurred in server thread (ClassNotFoundException)

java - 返回 null 或抛出异常

java - 构造函数抛出异常后可以调用finalize吗?

c++ - 我只是期待打印 char 值。发生了什么事?

c++ - 为什么输出的每一行之前都有不必要的空格?

c++ - 打印字符串 vector 的映射