【问题标题】:include iostream breaks uint32_t definition (C) [closed]包括 iostream 中断 uint32_t 定义(C)[关闭]
【发布时间】:2019-03-15 00:53:04
【问题描述】:

编辑:请忽略这个问题。我意识到我是个白痴,非常友善和乐于助人的人指出 iostream 不是 C 而是 C++ 库。

我遇到了一个很奇怪的问题。

我有一个完整的程序(大约 1000 行)。我需要

#include <iostream> 

当我这样做时,uint32_t 的 typedef 会中断。

是这样定义的

typedef unsigned __int32 uint32_t;

我正在使用 Visual Studio 2017。这是它给出的错误

\vc\tools\msvc\14.15.26726\include\cstdlib(19):错误 C2061:语法 错误:标识符“noexcept”

当我将鼠标悬停在带有下划线的 uint32_t 上时,会说:

uint32_t 变量“uint32_t”不是类型名称

只评论一行

#include <iostream>

程序按预期编译并运行。

什么给了?

附加问题。我使用 uint32_t 的原因是为了保证我的变量是 32 位长,因为我正在做很多位操作。使用 unsigned int 会安全吗?

这是我所包含的所有内容的列表:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <iostream>

typedef unsigned __int32 uint32_t;

【问题讨论】:

  • 恐怕unsigned int 没有保证大小,就像普通的int 一样。
  • &lt;iostream&gt; 是 C++ 头文件,不能在 C 中使用。
  • 呜呜我是个白痴。谢谢 :) 谷歌搜索的东西没有意识到我在看 C++ 的东西 :)
  • 您不应该在 C 代码中包含 &lt;iostream&gt;,因为它是 C++ 标头,无论如何都不是 C 标准的一部分。
  • 致“有帮助”添加 C++ 标签的人:这隐藏了问题。标题正确地指出这是一道 C 题。

标签: c iostream uint32-t


【解决方案1】:

自己声明 uint32_t 是非法的 afaik。不要那样做。 C++ 有一个标准的。用那个。它在cstdint 中声明。

https://en.cppreference.com/w/cpp/types/integer

如果您在C,那么您需要使用的标头是stdint.h

https://en.cppreference.com/w/c/types/integer

【讨论】:

  • 这是 C,而不是 C++。同样适用?
  • @Duxa 以及您如何在 C 中使用 iostream ???
  • @Duxa 是的,它也适用于 C。除了标题是stdint.h
  • 是的,我是个白痴,我没有意识到我在看 C++ 文档。试图包含它来读取文件。需要找到一个C解决方案。谢谢你。而且我知道 stdint.h 但认为它会包含太多不需要的东西,所以选择使用 typedef 代替。很高兴知道这是非法的。
猜你喜欢
  • 2012-02-15
  • 1970-01-01
  • 2016-11-05
  • 1970-01-01
  • 2010-12-23
  • 2011-07-11
  • 2018-03-29
  • 2013-06-21
  • 2010-11-15
相关资源
最近更新 更多