关于C#:C – 在非结构或联合的东西中请求成员’x’


C - request for member ‘x’ in something not a structure or union

本问题已经有最佳答案,请猛点这里访问。

上下文:我正在尝试对一个叫做GrandTheftAutoV的游戏做一些修改。

我正在使用GET_ENTITY_COORDS函数获取游戏中实体的坐标:

1
2
3
4
5
6
7
static Inline struct Vector3 GET_ENTITY_COORDS(Entity entity, BOOL alive) {
    args[0] = entity;
    args[1] = alive;
    invokeNative(0x3FEF770D40960D5A);
    struct Vector3 vector3 = { *(float*)&rets[0], *(float*)&rets[1], *(float*)&rets[2] };
    return vector3;
}

这应该返回xPosyPoszPos

这就是我目前正在努力解决的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include"gta.h"

void invokeNative_s(u64 hash) {
    struct Native_s** g_Natives = (struct Native_s**)NativeTableAddress;
    struct Native_s* Natives = g_Natives[hash & 0xFF];
    while (Natives != 0) {
        for (unsigned int i = 0; i < Natives->NativeCount; i++) {
            if (Natives->NativeHashes[i] == hash) {
                ((void(*)(struct NativeArg_s*))Natives->NativeFunctions[i])((struct NativeArg_s*)FreeSpaceAddress);
                return;
            }
        }
        Natives = Natives->LastNativeTable;
    }
}

struct gtaVars_s {
    BOOL init;
    int frameCount;
};

static struct gtaVars_s *gtaVars = (struct gtaVars_s*)FreeSpaceAddress+0x100;

BOOL nativeHook() {
    if (!gtaVars->init) {
        nativeArg->ArgValues = args;
        nativeArg->ReturnValue = rets;
        gtaVars->frameCount = 0;
        gtaVars->init = TRUE;
    }

    int newFrameCount = GET_FRAME_COUNT();
    if (gtaVars->frameCount < newFrameCount) {
        gtaVars->frameCount = newFrameCount;

        Ped pedID = PLAYER_PED_ID();
        Player playerID = PLAYER_ID();

        /* define player coords */
        Vector3 playerCoords = GET_ENTITY_COORDS(pedID, 1);
        char waterRadius = 10;

        SET_SUPER_JUMP_THIS_FRAME(playerID);
        SET_PLAYER_INVINCIBLE(playerID, TRUE);

        if (IS_CONTROL_JUST_PRESSED(0, Button_Tpad)) {
            // teleport to beach
            SET_ENTITY_COORDS(pedID, -1374.881, -1398.835, 6.141, FALSE, FALSE, FALSE, TRUE);

            // set water
            MODIFY_WATER(playerCoords.x, playerCoords.y, waterRadius, 10);
            MODIFY_WATER(playerCoords.x+2, playerCoords.y, waterRadius, 10);
            MODIFY_WATER(playerCoords.x, playerCoords.y+2, waterRadius, 10);
            MODIFY_WATER(playerCoords.x+2, playerCoords.y+2, waterRadius, 10);
            MODIFY_WATER(playerCoords.x+4, playerCoords.y, waterRadius, 10);
            MODIFY_WATER(playerCoords.x, playerCoords.y+4, waterRadius, 10);
            MODIFY_WATER(playerCoords.x+4, playerCoords.y+4, waterRadius, 10);
            MODIFY_WATER(playerCoords.x+6, playerCoords.y, waterRadius, 10);
            MODIFY_WATER(playerCoords.x, playerCoords.y+6, waterRadius, 10);
            MODIFY_WATER(playerCoords.x+6, playerCoords.y+6, waterRadius, 10);
            MODIFY_WATER(playerCoords.x+8, playerCoords.y, waterRadius, 10);
            MODIFY_WATER(playerCoords.x, playerCoords.y+8, waterRadius, 10);
            MODIFY_WATER(playerCoords.x+8, playerCoords.y+8, waterRadius, 10);
        }
    }

    return TRUE;
}

但我从GET_ENTITY_COORDS函数中得到了多个错误,尽管我在脚本中定义了上述函数:

1
2
3
4
5
6
7
source/gta.c:42:3: error: unknown type name ‘Vector3’
   Vector3 playerCoords = GET_ENTITY_COORDS(pedID, 1);
   ^
source/gta.c:42:26: error: incompatible types when initializing type ‘int’ using type ‘struct Vector3’
   Vector3 playerCoords = GET_ENTITY_COORDS(pedID, 1);
                          ^
source/gta.c:53:29: error: request for member ‘x’ in something not a structure or union

有人知道这个错误的原因是什么吗?我该如何修复它?我会非常感激,因为我已经为此挣扎了好几天,我很想看到它解决了,提前谢谢你。


如您的一些片段所示,Vector3的用法如下:

1
static Inline struct Vector3 GET_ENTITY_COORDS

因此,由于这是可行的,它意味着它被定义为(在某些代码中,我们看不到)struct,而不是typedef struct(允许在没有struct关键字的情况下使用它)。

您的错误是在将变量声明为Vector3时:

1
Vector3 playerCoords = GET_ENTITY_COORDS(pedID, 1);

应该是

1
struct Vector3 playerCoords = GET_ENTITY_COORDS(pedID, 1);

其余的错误只是那个错误的结果。此时解析程序丢失。