使用cout输出cerr [英] Outputting cerr using cout

查看:106
本文介绍了使用cout输出cerr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一段基本执行以下操作的代码:

  #include< iostream> 

使用命名空间std;
int main()
{
cout<< cerr<< 嗨。

返回0;
}

输出:

  0x601088您好。 

首先,为什么有人会做cout<< cerr,这没有任何意义。
首先,上面的输出是什么意思?



值得一提的是,在我的机器上,上面的代码可以正确编译并执行。 p>

但是,在运行相同版本的gcc 5.4.0的另一台计算机(服务器ssh连接)上执行更复杂的代码(与上述操作相同)时,会产生此错误做make(为简化起见,简称:)

 错误: operator<<不匹配(操作数类型为 std: :ostream {aka std :: basic_ostream< char>}}和'std :: ostream {aka std :: basic_ostream< char>}')
cout<< cerr<< DB:字段 + e.table + [ + e.index +]。 + e.field

对此有何想法?

解决方案

直到c ++ 11, std :: basic_ios 提供了隐式转换为 void * 。此代码无法在c ++ 11或更高版本中编译。基本上,您可以使用gcc的较早版本进行编译:

  #include< iostream> 
int main()
{
void * x = std :: cerr;
std :: cout<< x<< 嗨。

返回0;
}


I came across a piece of code that does basically the following:

#include <iostream>

using namespace std;
int main()
{
    cout << cerr << " Hi.";

    return 0;
}

Output:

0x601088 Hi.

First of all, why would anyone do 'cout << cerr' it does not make sense. Second of all, what is the meaning of the output above?

Worth to mention that on my machine the above code compiles and executes without errors.

However a much more complex code (doing the same thing as above) on a different machine (server ssh connection) running the same version of gcc 5.4.0, produces this error when doing make (shortened for clarity):

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

Any thoughts on this?

解决方案

Until c++11, std::basic_ios offered an implicit conversion to void*. This code won't compile with c++11 or later. You basically have this, which compiles with older versions of gcc :

#include <iostream>
int main()
{
    void * x = std::cerr;
    std::cout << x << " Hi.";

    return 0;
}

这篇关于使用cout输出cerr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆