【发布时间】:2017-02-27 08:39:36
【问题描述】:
我有一个奇怪的运算符
#include <iostream>
#include <stdexcept>
inline std::ostream &operator<<(std::ostream &o, const std::exception &ex) {
o << ex.what();
return o;
}
namespace ns {
struct Struct { int m; };
inline std::ostream &operator<<(std::ostream &o, const Struct &s) {
o << s.m;
return o;
}
void fn() {
std::cout << Struct{ 1 } << std::endl;
try {
throw std::runtime_error("...");
} catch (std::exception &ex) {
std::cout << ex << std::endl;
}
}
}
int main() {
return 0;
}
编译器找不到 std::exception 的 operator
- 删除重载运算符
- 如果我将所有代码从命名空间 ns 移动到全局命名空间
代码编译。这种行为的原因是什么?
【问题讨论】:
-
是什么阻止了您发布编译器消息/错误?
-
@Nawaz:我不知道是什么阻止了 OP,但一个原因可能是错误消息,当我现在在 GCC 中尝试时,错误消息是 202 行和 16799 个字符。 Bjarne 无法让他重载的隐式空格获得批准,但 C++ 确实设法用它的错误消息来欺骗我们......
-
对不起:VS2015 说:严重性代码描述项目文件行抑制状态错误 C2679 二进制“.
-
@hob-B1T:在问题本身中发布错误消息。它是问题本身的部分。
标签: c++ operator-keyword