1

私は VC++ を使用して C で 128 ビット整数の「クラス」を記述しています (ヘッダーを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);
}

基本的に、十分に明確でない場合は、指定された の負の値 (-x) を見つけint128_tます。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;

および関数プロトタイプ:

/* 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 ライブラリはlibxml2、iTunes データベースで使用します ( C:\Users\{YOU}\My Music\iTunes\iTunes Library.xml)

関数を書き直して、奇妙な Unicode のがらくた (人々が R2L 文字でトロールするようなもの) ではないことを確認しましたが、役に立ちませんでした。私はやってみint128_t inv = *ret;ましたが、それはただ与えます:

Error   27  error C2440: '=' : cannot convert from 'int128_t' to 'int'  d:\libplist\libplist\include\int128.h   313 1   libplist
4

1 に答える 1

2
int128_t one;
int128_from_i8(&one, 1);

int128_t inv;
int128_inv(&inv, arg);

C89 ではすべての変数を関数の先頭で宣言する必要があり、Visual C は C89 のみをサポートするため、これは有効な 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 を使用している場合、前述の理由により、C99 ではなくC89です。Microsoft は、C89 以降の C 標準をサポートすることは決してないと述べています。これは、努力する価値がないと感じており、代わりに誰もが C++ または C# を使用する必要があるためです。私はそれが好きではありませんが、それについては何もできません。なんにしても。

于 2012-11-14T20:24:38.083 に答える