C ++ Struct:const字段更改

时间:2017-07-01 11:30:01

标签: c++ struct

我试着尽可能简短地解释我的问题,顺便说一句,我总是希望自己找到解决方案,但我正在寻找这个简单的错误超过5个小时。

想象一下,我有一个struct Struct:

CompletableFuture

好吧,不,我不会像这样在我的矢量中添加一个对象Struct:

struct Struct {

Struct(int i) : number(i){
    //number == 1
}

const int number;

void function() {
    //number != 1
}
};
vector<Struct*> structs;

好吧,这没有用,所以我调试并发现我的 const int number ,在构造函数中设置为1,当我调用函数时,它会变为完全不同的值( )。

Debugger in Constructor 此时,我正在使用 number = 1 初始化我的结构。 我知道, 0x41674FEE58 是结构的内存地址。

Debugger in function 在这一点上,我在这个功能。如您所见,数字已更改为32762,即使它是完全相同的结构(而不​​是副本),因为它具有相同的内存地址 0x41674FEE58

我知道这不是一个直截了当的问题,但也许有人在过去遇到了类似的问题,也许她/他的解决方案对我有用。或者我只是一个完全白痴,犯了一个明显的错误,应该坚持使用Java。

无论如何,谢谢你宝贵的时间。

//修改

这是我正在使用的代码,它应该成为一个目标机器人,我正在从内存中读取值。

Struct struct(1);
structs.push_back(&struct);
//foreach cause there gonna be more structs added soon, but error occurs at the first anyway
for(Struct* ptr:structs) ptr->function();

这是它开始的地方,CPU_Players被添加到Local_Player.read_Memory()

while (READ_WRITE_SUCCESS) {
    Local_Player.read_Memory();
    for (CPU_Player* p : cpu_players) (*p).read_Memory();
    //cout << cpu_players.size() << endl;
    Sleep(FREQUENCY);
}

这是你自己玩家的结构。我检查了playercount,如果它改变了,我用if语句添加/删除玩家。

//struct for player
struct Player {

Player() {
    coordinates.push_back(0.0f);
    coordinates.push_back(0.0f);
    coordinates.push_back(0.0f);
}

INT64 BaseAddress;
vector<float> coordinates;
char team[8] = { 0 };
float mouse_x;
float mouse_y;

int playercount;

void read_Memory() {

    //get current player baseaddress
    READ_WRITE_SUCCESS = ReadProcessMemory(process, sauerbratenexe + Offset_PlayerBase / sizeof(DWORD), &BaseAddress, sizeof(INT64), 0);
    //xyz
    ReadProcessMemory(process, (DWORD*)(BaseAddress + Offset_Position_x), &coordinates[0], sizeof(float), 0);
    ReadProcessMemory(process, (DWORD*)(BaseAddress + Offset_Position_y), &coordinates[1], sizeof(float), 0);
    ReadProcessMemory(process, (DWORD*)(BaseAddress + Offset_Position_z), &coordinates[2], sizeof(float), 0);
    //teamname
    ReadProcessMemory(process, (DWORD*)(BaseAddress + Offset_TeamName), &team, 8, 0);
    //playercount
    ReadProcessMemory(process, sauerbratenexe + Offset_PlayerCountBase / sizeof(DWORD), &playercount, sizeof(int), 0);
    --playercount;

    if (playercount != cpu_players.size() && cpu_players.size() >= 0) {
        int diff = playercount - cpu_players.size();

        //players joined
        if (diff > 0) {
            while (diff > 0) {


                //here i add my CPU_Player struct
                CPU_Player cpu(cpu_players.size()+1);
                cpu_players.push_back(&cpu);
                cout << "cpu" << cpu_players.size() << " added" << endl;
                diff--;
            }
        }//players left
        else {
            while (diff < 0) {
                cout << "cpu" << cpu_players.size() << " removed" << endl;
                cpu_players.pop_back();
                diff++;
            }
        }
    }
}
} Local_Player;

这是CPU_Player,其中const int数被弄乱了。

1 个答案:

答案 0 :(得分:1)

        while (diff > 0) {
            //here i add my CPU_Player struct
            CPU_Player cpu(cpu_players.size()+1);
            cpu_players.push_back(&cpu);
            cout << "cpu" << cpu_players.size() << " added" << endl;
            diff--;
        }

名为cpu的对象的生命周期以大括号}结束,结束while循环。你已经将一个指向它的指针放入一个向量中,但是该对象已经死了,并且从while循环的下一次迭代开始,使用指针是无效的。

您的代码没有证明使用指针的任何理由,所以不要。声明std::vector<CPU_Player> cpu_players;,然后执行cpu_players.push_back(cpu);。这会将名为cpu的对象复制到向量所拥有的另一个CPU_Player对象中。

如果您以后确实有某些理由使用指针(可能CPU_Player成为多态基类,并且您希望将派生类对象放在向量中),请改用智能指针。这看起来像

std::vector<std::unique_ptr<CPU_Player>> cpu_players;

cpu_players.push_back(std::make_unique<CPU_Player>(cpu_players.size()+1));

unique_ptr为您管理指向对象的生命周期,因此您很少需要担心它。