gpt4 book ai didi

c++ - 重新定义 uint 类型 c++

转载 作者:行者123 更新时间:2023-11-28 01:42:27 24 4
gpt4 key购买 nike

c++ 中,当我尝试编译以下代码时出现声明冲突错误:

#include <iostream>
using namespace std;

typedef uint_least64_t uint;

int main() {
uint i = -1;
cout << i << endl;
}

错误:

main.cpp:5:24: error: conflicting declaration ‘typedef uint_least64_t uint’
typedef uint_least64_t uint;
^~~~
In file included from /usr/include/stdlib.h:275:0,
from /usr/include/c++/6/cstdlib:75,
from /usr/include/c++/6/ext/string_conversions.h:41,
from /usr/include/c++/6/bits/basic_string.h:5417,
from /usr/include/c++/6/string:52,
from /usr/include/c++/6/bits/locale_classes.h:40,
from /usr/include/c++/6/bits/ios_base.h:41,
from /usr/include/c++/6/ios:42,
from /usr/include/c++/6/ostream:38,
from /usr/include/c++/6/iostream:39,
from main.cpp:1:
/usr/include/x86_64-linux-gnu/sys/types.h:152:22: note: previous declaration as ‘typedef unsigned int uint’
typedef unsigned int uint;
^~~~

我假设声明冲突错误是因为语言中某个地方已经存在 uint 的类型声明,并且我认为该类型是 uint_least32_t 因为:

#include <iostream>
using namespace std;

int main() {
uint i = -1;
cout << i << endl;
}

返回一个整数值,即 (2^32)-1。因此,是否有可能在 c++ 中将 uint 重新定义为 uint_least64_t

最佳答案

从报错信息可以看出,在你的平台上<iostream>拉入 <string> , 拉入 <cstdlib> , 拉入 <stdlib.h> , 拉入 <sys/types.h> .

查看源代码,我们可以看到<sys/types.h>这样做:

#ifdef __USE_MISC
/* Old compatibility names for C types. */
typedef unsigned long int ulong;
typedef unsigned short int ushort;
typedef unsigned int uint;
#endif

#ifdef __USE_MISC守卫是 feature_test_macros 的一部分系统。 __USE_MISC如果您请求 _DEFAULT_SOURCE,则在内部设置(如果您没有其他要求,也会设置)。

因此,您应该能够通过使用 -ansi 进行编译来绕过该问题。或 -std=... 之一选项(例如 -std=c++11 )。

关于c++ - 重新定义 uint 类型 c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46623622/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com