C++代码编译失败,错误"长类型名称"无效
C++ code fails to compile with error 'long type-name' is invalid
我更喜欢避免使用typedef
,而更喜欢使用using
,但我偶然发现了必须使用它的情况,因为thrift
(0.9.3版本)输出的代码使用了typedef
。错误的最小实例出现在以下代码中
#include <iostream>
using namespace std;
typedef int64_t long;
typedef int32_t int;
int main() {
cout << "Hello world " << endl;
return 0;
}
我得到的错误是
test.cpp:4:17: error: 'long type-name' is invalid
typedef int64_t long;
^
test.cpp:4:1: error: typedef requires a name [-Werror,-Wmissing-declarations]
typedef int64_t long;
^~~~~~~~~~~~~~~~~~~~
test.cpp:5:17: error: cannot combine with previous 'type-name' declaration specifier
typedef int32_t int;
^
test.cpp:5:1: error: typedef requires a name [-Werror,-Wmissing-declarations]
typedef int32_t int;
^~~~~~~~~~~~~~~~~~~
4 errors generated.
我从g++ --version
得到的输出是
Apple LLVM version 7.3.0 (clang-703.0.31)
Target: x86_64-apple-darwin15.4.0
Thread model: posix
有人能帮我解决这个错误吗?
long
是c++中的一个关键字,因此不能创建名称为long
的类型。请参阅列表。
但问题在于Thrift生成的代码。我用Thrift做了一些实验,我可以通过在官方tutorial.thrift
文件中添加这一行来重现这个问题:
typedef i64 long
显然Thrift不会检查这是否会编译。因此,您需要确保您的typedef适用于所有可能的语言。
应该是
typedef long int64_t;
typedef int int32_t;
Typedef的工作方式类似于变量声明,只是前面有typedef
。
相关文章:
- 具有引用返回类型的重写方法上的协变返回类型无效
- 使用 std::future 的不完整类型无效使用
- 类指针和类指针指向二进制运算符+的类型无效的操作数
- 静态类模板成员:将"sizeof"应用于不完整类型无效
- 数组的类型无效
- 返回并强制转换数组指针(错误:数组下标的类型..无效)
- 错误:数组下标'double*[double]'的类型无效
- 数组下标错误的类型无效
- 协变返回类型无效的转换错误
- 在代码块错误中收到此错误:数组下标'main()::fields[int]'的类型无效
- 3 错误:错误:未在此范围内声明'Entry'。错误:模板参数 1 无效。错误:令牌之前声明中的类型无效'('
- 类成员数组的实现:数组下标的类型无效
- 别名模板、部分专用化和无效参数类型无效
- 数组下标'int [5][float]'的类型无效
- 数组下标的类型无效
- 是否存在对类型名称有效但对基本类型无效的语言构造
- 编译错误 - 数组下标'char[int]'的类型无效
- 使用指向结构数组的指针的参数类型无效
- 使用类模板初始化下标重载函数中的引用类型无效
- 对模板和decltype使用不完整类型无效