std :: cout不喜欢std :: endl和有条件的条件下的字符串 [英] std::cout doen't like std::endl and string in conditional-if

查看:62
本文介绍了std :: cout不喜欢std :: endl和有条件的条件下的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

main.cpp: In function ‘void PrintVector(std::vector<std::__cxx11::basic_string<char> >&, bool)’:
main.cpp:16:41: error: overloaded function with no contextual type information
  std::cout << ((newline)? (std::endl) : "");
                                         ^~

为什么std :: cout不喜欢std :: endl和字符串在条件条件下吗?

Why std::cout doen't like std::endl and string in conditional-if?

推荐答案

我将其更改为:

std::cout << (newline? "\n" : "") << std::flush;

不可能用'(会更快)来写它:

It is not possible to write it with ' (would be faster):

std::cout << (newline? '\n' : '') << std::flush;

因为为空,并导致错误:空字符常量。

because '' is empty and leads to "error: empty character constant".

带条件的条件的解决方案是如此复杂,以至于人们应该首选以下条件:

The solution with the conditional-if is so complex that one should prefer the following:

if (newline) std::cout << std::endl;

这篇关于std :: cout不喜欢std :: endl和有条件的条件下的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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