我正在使用VC++在C中编写一个128位整数“类”(将 header 包装在extern "C"
块中),但是在例程构建过程中检查错误时,添加此函数后遇到了很多问题:
/* Negate (2s compliment NOT) a 128-bit integer */
INT128_INLINE void int128_neg(int128_t *ret, int128_t arg)
{
/* 2s compliment negation is equivalent to ((~value) + 1) */
int128_t one;
int128_from_i8(&one, 1);
int128_t inv;
int128_inv(&inv, arg);
int128_add(ret, one, inv);
}
本质上,如果不够清晰,它将找到给定
int128_t
的负数(-x)。这是int128_t
结构:/* Work at the byte level to work around endianess crap */
/* (speaking of which, this is big endian, at least is should be) */
#pragma pack(1)
struct _int128_t
{
/* SIGN BIT: 0xxxxxxx = positive; 1xxxxxxx = negative */
uint8_t byte01; /* MSB */
uint8_t byte02;
uint8_t byte03;
uint8_t byte04;
uint8_t byte05;
uint8_t byte06;
uint8_t byte07;
uint8_t byte08;
uint8_t byte09;
uint8_t byte10;
uint8_t byte11;
uint8_t byte12;
uint8_t byte13;
uint8_t byte14;
uint8_t byte15;
uint8_t byte16; /* LSB */
};
/* A 128-bit integer structure and functions written in pure C99 code :) */
typedef struct _int128_t int128_t;
和函数原型(prototype):
/* Invert (~) a 128-bit integer */
INT128_INLINE void int128_inv(int128_t *ret, int128_t arg) { ... }
/* Add two 128-bit integers together */
INT128_INLINE void int128_add(int128_t *ret, int128_t lhs, int128_t rhs) { ... }
和
INT128_INLINE
(显然使用了将其设置为__inline
的代码):/* MC++ requires `__inline`*/
#ifdef _MSC_VER
# define INT128_INLINE __inline
#else
/* use standard inline */
# define INT128_INLINE inline
#endif
现在,我通过函数收到的错误是:
Error 24 error C2275: 'int128_t' : illegal use of this type as an expression d:\libplist\libplist\include\int128.h 313 1 libplist
Error 25 error C2146: syntax error : missing ';' before identifier 'inv' d:\libplist\libplist\include\int128.h 313 1 libplist
Error 26 error C2065: 'inv' : undeclared identifier d:\libplist\libplist\include\int128.h 313 1 libplist
Error 28 error C2065: 'inv' : undeclared identifier d:\libplist\libplist\include\int128.h 314 1 libplist
Error 29 error C2065: 'inv' : undeclared identifier d:\libplist\libplist\include\int128.h 316 1 libplist
Error 30 error C2440: 'function' : cannot convert from 'int' to 'int128_t' d:\libplist\libplist\include\int128.h 316 1 libplist
Error 32 error C2371: 'int128_inv' : redefinition; different basic types d:\libplist\libplist\include\int128.h 324 1 libplist
在您询问之前,第313行是
int128_t inv;
。而且,libplist
是我正在使用的Apple PList库,它使用iTunes数据库中的libxml2
(C:\Users\{YOU}\My Music\iTunes\iTunes Library.xml
)我尝试重写该函数,以确保它不是一些奇怪的Unicode废话(就像人们确实喜欢R2L char一样),但是它没有帮助。我尝试做
int128_t inv = *ret;
,但这只是给:Error 27 error C2440: '=' : cannot convert from 'int128_t' to 'int' d:\libplist\libplist\include\int128.h 313 1 libplist
最佳答案
int128_t one;
int128_from_i8(&one, 1);
int128_t inv;
int128_inv(&inv, arg);
这不是有效的C89代码,因为C89要求所有变量都在函数顶部声明,而Visual C仅支持C89。声明必须出现在函数中的语句之前。因此,如果您真正使用的是C编译器(而不是C++编译器),则可能会导致错误。您应该做的是:
int128_t one;
int128_t inv;
int128_from_i8(&one, 1);
int128_inv(&inv, arg);
另外,此评论不正确:
/* A 128-bit integer structure and functions written in pure C99 code :) */
如果您使用的是Visual C,则由于上述原因,它是 C89 而不是C99。微软表示,他们永远不会支持C89之后的任何C标准,因为他们认为这样做不值得,并且每个人都应该使用C++或C#。我不喜欢它,但是对此我无能为力。所以,随便
关于c - 多个VC编译错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13373662/