非法,左操作数的类型为“ DWORD [29]”

时间:2018-09-17 22:04:05

标签: c++ enums

我刚接触C ++,真的很困在这里。

if (bAk == 1)
{
    int fireRate = 134;

    if (shotTiming < 30)
    {
        int valueX = (AssaultRifle::recoilTableX[shotTiming] * 0.48) + shakerNum;
        int smoothingX = valueX / 5;
        int valueY = (AssaultRifle::recoilTableY[shotTiming] * 0.48) + shakerNum;
        int smoothingY = valueY / 5;
        Sleep(3);

        for (int i = 0; i < 5; i++)
        {
            mouse_move(valueX, valueY);
            Sleep(fireRate / 5);
        }
        shotTiming++;
        cout << valueX;
    }
}

我此时遇到的唯一构建错误是illegal, left operand has type 'DWORD [29]' recoilTable的两个int值都表示AssaultRifle命名空间必须具有arithmetic or unscoped enum type。我只需要向正确的方向完成它即可。

#pragma once
#include <Windows.h>

namespace AssaultRifle
{
    const size_t MAX_INDEX_WEAPON = 1;
    const size_t MAX_INDEX_RECOIL = 29;
    DWORD recoilTableY[MAX_INDEX_WEAPON][MAX_INDEX_RECOIL] = {
        { 40, 48, 48, 48, 33, 33, 28, 24, 16, 13, 18, 22, 24, 29, 33, 33, 33, 29, 22, 20, 17, 17, 17, 17, 20, 27, 27, 27, 26 }
    };
    DWORD recoilTableX[MAX_INDEX_WEAPON][MAX_INDEX_RECOIL] = {
        { -36, 5, -59, -49, 3, 20, 25, 45, 43, 32, 82, 8, 43, -32, -25, -40, -35, -32, -43 , -42, -42, -55, -25, 15, 20, 35, 50, 62, 40 }
    };
}

1 个答案:

答案 0 :(得分:2)

您的recoilTableXrecoilTableY数组都是二维 1

DWORD recoilTableY[<# elements in first dimension>][<# elements in second dimension>] = {...};
DWORD recoilTableX[<# elements in first dimension>][<# elements in second dimension>] = {...};

但是,当从数组中读取单个值时,您的代码仅索引到第一维度。这就是为什么会出现错误的原因,因为您无法像尝试那样将整个数组作为单个整数访问。您必须为所有可用尺寸指定索引。

更改此:

AssaultRifle::recoilTableX[shotTiming]
AssaultRifle::recoilTableY[shotTiming]

为此:

AssaultRifle::recoilTableX[0][shotTiming]
AssaultRifle::recoilTableY[0][shotTiming]

MAX_INDEX_WEAPON为1,因此数组的第一维上只有一个插槽,因此第一维中唯一有效的索引为0,这使得第一维几乎没有用,应该删除它,除非您计划在将来为其他武器增加价值。

MAX_INDEX_RECOIL是29,所以数组第二维中有29个插槽,因此第二维中唯一有效的索引为0..28(含0),但是您的代码允许访问索引29。

MAX_INDEX_WEAPONMAX_INDEX_RECOIL常量的 NAMES 具有误导性,因为它们实际上根本没有用作索引。

1:同样,您的数组应声明为const

尝试以下方法:

#pragma once
#include <Windows.h>

namespace AssaultRifle
{
    const size_t MAX_WEAPONS = 1;
    const size_t MAX_RECOILS = 29;
    const int recoilTableY[MAX_WEAPONS][MAX_RECOILS] = {
        { { 40, 48, 48, 48, 33, 33, 28, 24, 16, 13, 18, 22, 24, 29, 33, 33, 33, 29, 22, 20, 17, 17, 17, 17, 20, 27, 27, 27, 26 } }
    };
    const int recoilTableX[MAX_WEAPONS][MAX_RECOILS] = {
        { { -36, 5, -59, -49, 3, 20, 25, 45, 43, 32, 82, 8, 43, -32, -25, -40, -35, -32, -43 , -42, -42, -55, -25, 15, 20, 35, 50, 62, 40 } }
    };
}

if (bAk == 1)
{
    int fireRate = 134;

    if (shotTiming < AssaultRifle::MAX_RECOILS)
    {
        int valueX = (AssaultRifle::recoilTableX[0][shotTiming] * 0.48) + shakerNum;
        int smoothingX = valueX / 5;
        int valueY = (AssaultRifle::recoilTableY[0][shotTiming] * 0.48) + shakerNum;
        int smoothingY = valueY / 5;
        Sleep(3);

        for (int i = 0; i < 5; i++)
        {
            mouse_move(valueX, valueY);
            Sleep(fireRate / 5);
        }
        shotTiming++;
        cout << valueX;
    }
}