std :: ostream不能输出一个const char数组吗?

J. *_*lan 2 c++ operator-overloading ostream

为了它的乐趣和体验,我是 修改和探索Blobby Volley 2 1.0(Linux)的源代码.

嗯...我修改源代码,但我甚至无法编译程序.(伤心,不是吗?)

这是导致错误的代码:

std::ostream& operator<<(std::ostream& stream, const ServerInfo& val) {
    return stream << val.name << " (" << val.hostname << ":" << val.port << ")";
    }
Run Code Online (Sandbox Code Playgroud)

尝试使用g ++ 5.4.0编译它会产生以下内容(简化输出 - 原始输出为~443行)错误消息:

错误:'operator <<'不匹配(操作数类型为'std :: ostream {aka std :: basic_ostream}'和'const char [32]')

return stream << val.name <<"("<< val.hostname <<":"<< val.port <<")";

我将代码简化为:

std::ostream& operator<<(std::ostream& stream, const ServerInfo& val) {
    stream << "hello"; //can't get simpler than this, right?
    return stream;
    }
Run Code Online (Sandbox Code Playgroud)

得到了

错误:'operator <<'不匹配(操作数类型为'std :: ostream {aka std :: basic_ostream}'和'const char [6]')

stream <<"你好";

调用它的代码如下所示:

std::cout << "duplicate server entry\n";
std::cout << info << "\n"; //it's called here
Run Code Online (Sandbox Code Playgroud)

我发现最令人惊讶的是,我们都知道,std::cout它的同类可以处理char数组.

例如,

#include <iostream>
#include <string>

int main () {
    const char a[6] = "hello";
    std::cout << a << std::endl; //No problem here!
    return 0;
    }
Run Code Online (Sandbox Code Playgroud)

毫无障碍地工作.


哦,还有一件事.

如果我包括<string>,这有效:

std::ostream& operator<<(std::ostream& stream, const ServerInfo& val) {
    stream << std::string("hello");
    return stream;
    }
Run Code Online (Sandbox Code Playgroud)

有谁知道我错过了什么?


PS:是错误pastebin.

PPS:这是请求的标头:

/* header include */
#include "NetworkMessage.h"

/* includes */
#include <cstring>

#include "UserConfig.h"
#include "SpeedController.h"
Run Code Online (Sandbox Code Playgroud)

PPS:如果您想知道为什么我没有得到关于std::ostream未定义的错误,请查看Sam的答案的第3段.

Sam*_*hik 12

#include <iostream>可能缺失的事实是用Sherlock Holmes的调试方法推断出来的:"当你消除了不可能的事情,无论剩下什么,无论多么不可能,都必须是真理".

显然.std::ostream应该没有接受const char *过载的问题.

因此,重载决议投诉必须意味着<iostream>不包括在内.大多数C++库类都是在整个地方进行前向声明.包括一些随机头文件很可能会获得一个前向声明std::ostream,作为免费奖金.所以编译器不会抱怨这个类没有被定义.

但除非<iostream>包含,否则编译器将不知道那里定义的所有重载.而已.

  • `#include &lt;ostream&gt;`(声明 `const char*` 重载 - 27.7.3.1)代替 `iostream`(包括 `ostream` - 27.4.1)就足够了。 (2认同)