简体   繁体   中英

Create a array of structs

I have a problem with my code. It is intended to save the values into a struct array. But 2 things happen at random - 1) Array may be empty or 2) Array may contain only one row of values.

struct MacroMas
    {
        int x;
        int y;
        int Delay;
        int SemiAutoDelay;
        int ammo;

        MacroMas* Cords(int x, int y, int Delay)
        {
            MacroMas _ret;
            _ret.x = x;
            _ret.y = y;
            _ret.Delay = Delay;

            return _ret;
        }
    };

    MacroMas* temp()
    {
        MacroMas _ret;
        MacroMas* macroMasArray = new MacroMas[107];
        for (int index = 0; index <107 ; ++index)
            macroMasArray[index] = MacroMas();
        macroMasArray[0].Cords(-3, 4, 16);
        macroMasArray[1].Cords(-3, 4, 17);
        // Some more code
        return macroMasArray;

First in Cords function: function will return MacroMas* and you return an object of type MacroMas it's mistake in your Code.

in these two lines you make a mistake

macroMasArray[0].Cords(-3, 4, 16);
macroMasArray[1].Cords(-3, 4, 17);

Cords function has return value When macroMasArray[0] or [1] calls Cords its not affect on them. you need to store them like

macroMasArray[0] = macroMasArray[0].Cords(-3, 4, 16);

or use this pointer in the Cords Body.

void Cords(int x, int y, int Delay)
{
    this->x = x
    this->y = y;
    this->Delay = Delay;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM