gcc std::istream '错误:从类型 ‘std::streamsize’ 进行无效转换输入“std::streamsize”

发布于 2024-10-16 22:14:39 字数 871 浏览 2 评论 0原文

好吧,这是一个非常奇怪的情况。我正在将原始数据读入缓冲区,没什么花哨的,我的代码是这样的:

typedef unsigned char Byte;
/* ... */
static Byte SerializeBuffer[2048];
/* ... */
std::streamsize readInBuffer = 
                data.read((char*)SerializeBuffer, sizeof(SerializeBuffer));

但我会不断收到编译错误消息 'error: invalidcast from type 'void *' to type 'std::streamsize'',不知道为什么编译器认为 sizeof 是一个 void 指针。好吧,我尝试用多种方式进行转换,但同样的错误不断发生。我最终得到了这个:

std::streamsize dummy = sizeof(SerializeBuffer);
std::streamsize readInBuffer = 
                data.read((char*)SerializeBuffer, reinterpret_cast<std::streamsize>(dummy));

弹出以下内容: error: invalidcast from type 'std::streamsize' to type 'std::streamsize'

我完全不知所措。还有其他想法吗?

编译器:gcc 4.4.5
操作系统:Linux 2.6.35

编辑: Visual Studio 2010 上也是如此

Alright, so here is a really weird one. I am reading raw data into a buffer, nothing fancy, my code went like so:

typedef unsigned char Byte;
/* ... */
static Byte SerializeBuffer[2048];
/* ... */
std::streamsize readInBuffer = 
                data.read((char*)SerializeBuffer, sizeof(SerializeBuffer));

But I would keep getting the compile error message 'error: invalid cast from type ‘void *’ to type ‘std::streamsize’', No idea why the compiler thought that sizeof was a void pointer. Well I tried casting it in several ways, but the same error kept happening. I ended up with this:

std::streamsize dummy = sizeof(SerializeBuffer);
std::streamsize readInBuffer = 
                data.read((char*)SerializeBuffer, reinterpret_cast<std::streamsize>(dummy));

Which pops up the following: error: invalid cast from type ‘std::streamsize’ to type ‘std::streamsize’

I am at a complete loss. Any other Ideas?

Compiler: gcc 4.4.5
OS: Linux 2.6.35

edit:
Same thing on Visual Studio 2010

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

書生途 2024-10-23 22:14:39

如果data是一个istream,请记住成员read返回对data的引用(流本身),而不是读取的字符数。

void * 内容可能是因为编译器将其分配给 std::streamsize 成员,尝试使用隐式转换为 void * > (当您执行 if(data) ... 时使用的那个),但仍然 void *std::streamsize 不太匹配

顺便说一句,在调用read之后,可以使用gcount 方法。

If data is an istream, keep in mind that the member read returns a reference to data (the stream itself), not the number of characters read.

The void * stuff is probably because the compiler, to assign it to the std::streamsize member, tries to use the implicit conversion to void * (the one that is used when you do if(data) ...), but still void * is not a good match for std::streamsize.

By the way, the information about the number of characters read can be obtained, after the call to read, using the gcount method.

_蜘蛛 2024-10-23 22:14:39

您应该查看文档。 Read 返回对流的引用。所以发生的事情是:

  1. 您调用 read,它返回一个 istream&。
  2. 您尝试将该 istream 分配给 std::streamsize。
  3. 由于编译器找不到合适的方法来执行此操作,因此它尝试将流运算符 void* 的结果分配给 std::streamsize。
  4. 由于您无法分配这些类型,因此会产生错误。

You should check the documentation. Read returns a reference to the stream. So what's happening is:

  1. You call read, which returns an istream&.
  2. You try to assign that istream to a std::streamsize.
  3. Since the compiler does not find a suitable way to do this, it tryes to assign the result of the stream's operator void* to your std::streamsize.
  4. Since you can't assign these types, an error is produced.
大海や 2024-10-23 22:14:39

它必须是 std::streamsize readInBuffer = data.read(... 部分。read 不返回大小,而是返回流本身。

It must be the std::streamsize readInBuffer = data.read(... part. read doesn't return size, but the stream itself.

橪书 2024-10-23 22:14:39

如果您想知道读取了多少字节,请使用 readsome() 而不是 read()

If you want to know how many bytes were read use readsome() not read()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文