使用 cout 输出 cerr
Outputting cerr using cout
我遇到了一段代码,基本上执行以下操作:
#include <iostream>
using namespace std;
int main()
{
cout << cerr << " Hi.";
return 0;
}
输出:
0x601088 Hi.
首先,为什么会有人做'cout << cerr'这没有意义。
其次,上面的输出是什么意思?
值得一提的是,上面的代码在我的机器上编译和执行没有错误。
但是在另一台机器(服务器 ssh 连接)上的更复杂的代码(做与上面相同的事情)运行 相同版本的 gcc 5.4.0,在执行 make 时会产生这个错误(缩短为清楚起见):
error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘std::ostream {aka std::basic_ostream<char>}’)
cout << cerr << "DB: Field " + e.table + "[" + e.index + "]." + e.field
对此有什么想法吗?
直到 c++11,std::basic_ios
提供了一个 implicit conversion to void*
。此代码无法使用 c++11 或更高版本进行编译。你基本上有这个,它用旧版本的 gcc 编译:
#include <iostream>
int main()
{
void * x = std::cerr;
std::cout << x << " Hi.";
return 0;
}
我遇到了一段代码,基本上执行以下操作:
#include <iostream>
using namespace std;
int main()
{
cout << cerr << " Hi.";
return 0;
}
输出:
0x601088 Hi.
首先,为什么会有人做'cout << cerr'这没有意义。 其次,上面的输出是什么意思?
值得一提的是,上面的代码在我的机器上编译和执行没有错误。
但是在另一台机器(服务器 ssh 连接)上的更复杂的代码(做与上面相同的事情)运行 相同版本的 gcc 5.4.0,在执行 make 时会产生这个错误(缩短为清楚起见):
error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘std::ostream {aka std::basic_ostream<char>}’)
cout << cerr << "DB: Field " + e.table + "[" + e.index + "]." + e.field
对此有什么想法吗?
直到 c++11,std::basic_ios
提供了一个 implicit conversion to void*
。此代码无法使用 c++11 或更高版本进行编译。你基本上有这个,它用旧版本的 gcc 编译:
#include <iostream>
int main()
{
void * x = std::cerr;
std::cout << x << " Hi.";
return 0;
}