小编J. *_*lan的帖子

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

为了它的乐趣和体验,我是 修改和探索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)

c++ operator-overloading ostream

2
推荐指数
1
解决办法
6207
查看次数

为什么我不能使用函数调用来声明数组的长度?

这就是我想要做的:

const char wav_folder[] = ".\\wav";
const char search_path[strlen(wav_folder) + 6];
Run Code Online (Sandbox Code Playgroud)

但是strlen部分不允许我创建这个字符数组.这不是编译.为什么?

错误:

Error   1   error C2057: expected constant expression   c:\users\hassan\documents\cpp\dir_search\dir_search\dir_search\source.cpp   15  1   dir_search
Error   2   error C2466: cannot allocate an array of constant size 0    c:\users\hassan\documents\cpp\dir_search\dir_search\dir_search\source.cpp   15  1   dir_search
Error   3   error C2734: 'search_path' : const object must be initialized if not extern c:\users\hassan\documents\cpp\dir_search\dir_search\dir_search\source.cpp   15  1   dir_search
Error   4   error C2133: 'search_path' : unknown size   c:\users\hassan\documents\cpp\dir_search\dir_search\dir_search\source.cpp   15  1   dir_search
Run Code Online (Sandbox Code Playgroud)

c++ arrays char strlen

1
推荐指数
1
解决办法
424
查看次数

标签 统计

c++ ×2

arrays ×1

char ×1

operator-overloading ×1

ostream ×1

strlen ×1