[英]std::ostream::operator<< not defined for std::string?
I have stumbled upon an error for which I cannot grasp the reason. 我偶然发现了一个我无法理解的错误。
I think it basically gets down to this error: 我认为它基本上归结为这个错误:
error: no matching function for call to ‘std::basic_ostream<char>::operator<<(const std::basic_string<char>&)’
I looked into specification on www.cplusplus.com and indeed it says there is no definition for std::ostream::operator<<
with std::string
as an argument. 我查看了www.cplusplus.com上的规范 ,实际上它说没有
std::ostream::operator<<
定义, std::string
作为参数。
My question is, what happens when one writes std_ostream_instance << std_fancy_string;
我的问题是,当写一个
std_ostream_instance << std_fancy_string;
. 。 I believe it is one of the most common invocations ( eg
std::out << std::string("Hello world!")
) next to const char*
. 我相信它是
const char*
旁边最常见的调用之一(例如std::out << std::string("Hello world!")
)。
The error originates from these lines: 错误源自以下行:
template<typename T>
void Log::_log(const T& msg)
{ _sink->operator<<( msg ); }
_sink
is defied as std::ostream*
There are some wrapping functions around but it breaks here. _sink
被定义为std::ostream*
有一些包装函数,但它在这里打破。
I think I could work around by writing 我想我可以通过写作来解决
template<>
void Log::_log<std::string>(const std::string& msg) {
_sink->operator<<( msg.c_str() );
}
since there is ostream& operator<< (ostream& out, const unsigned char* s );
因为有
ostream& operator<< (ostream& out, const unsigned char* s );
defined by default. 默认定义。
I just see no reason why it is not guessed automatically since it clearly works in simple use like cout << any_std_string
. 我没有看到没有理由为什么它没有自动猜测,因为它显然可以像
cout << any_std_string
一样简单使用。
Not sure if this is relevant but I want to be able to pass down through my log functions anything than can be handled by std::ostream
. 不确定这是否相关但我希望能够通过我的日志函数向下传递
std::ostream
可以处理的任何内容。 I used explicit non-templated declarations but decided to move to template for log(const T& anything_to_log)
to refacator it. 我使用了显式的非模板化声明,但决定转到
log(const T& anything_to_log)
模板来重新设计它。 It seemed plain stupid to have 5+ overloads. 拥有5+超载似乎很愚蠢。 I get the error when I try compiling something like
Log::log( std::string("test case") )
. 当我尝试编译
Log::log( std::string("test case") )
类的东西时,我收到错误。
It looks like something stupid-simple but I cannot get it on my own. 它看起来像一些愚蠢的简单但我不能靠自己得到它。 Tried to google and search stack to no avail.
试图谷歌和搜索堆栈无济于事。
With regards, luk32. 关于,luk32。
PS. PS。 I checked the work-around and it works.
我检查了解决方法并且它有效。 Why it's not implicitly done ?
为什么它没有隐含地完成?
operator <<
overloads are not members of ostream
. operator <<
overloads不是ostream
成员。 They are freestanding functions, for example 例如,它们是独立的功能
ostream& operator << ( ostream& out, const basic_string<T>& bs );
Try 尝试
template<typename T>
void Log::_log(const T& msg)
{ *_sink << msg; }
The std::string
version is not a member function, so can't be called as a member of _sink
. std::string
版本不是成员函数,因此不能作为_sink
的成员_sink
。 Try it this way to pick up both member and non-member versions (in fact it is unlikely you will need the member versions at all anyway): 尝试这种方式来获取成员和非成员版本(实际上你根本不需要成员版本):
#include <iostream>
#include <string>
int main()
{
std::ostream * os = &std::cout;
std::string s = "Hello\n";
// This will not work
// os->operator<<(s);
(*os) << s;
return 0;
}
Or better yet would be to store _sink
as a reference, and output exactly as you normally would to cout
. 或者更好的是将
_sink
存储为参考,并按照通常的cout
输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.