开发者

Multidimensional array of object in C++ , I can not initialize it!

开发者 https://www.devze.com 2023-01-13 15:36 出处:网络
Rookie C++ Programmer here again I\'m using VC++ VS2008 and making an attempt at creating an array of arrays. All objects I wish to store I want to put on the heap.

Rookie C++ Programmer here again

I'm using VC++ VS2008 and making an attempt at creating an array of arrays. All objects I wish to store I want to put on the heap.

In the arrays it's all just pointers.

Here's some code:

Grid.h

#include "Tile.h"

class Grid
{
public: 
    Tile* grid_ptr[8][8];
...
...
};

Grid.cpp

#include "stdafx.h"
#include "Grid.h"

...
.开发者_开发知识库..
void Grid::Initialize()
{
    for(int i = 0; i < 8; i++)
    {
        Grid::grid_ptr[i][0] = new Tile::Tile(10,10);
        for (int j = 0; j < 8; j++)
        {
            Grid::grid_ptr[i][j] = new Tile::Tile(10,10);
        }

    }
}
...
...
}

Everything works just fine including the tile construction. It seems like a syntax error seeing as the compiler gives me this

Error 1 error C2061: syntax error : identifier '{ctor}'

Error 2 error C2061: syntax error : identifier '{ctor}'

All the time the same story. This has my entire work bogged down to a halt unfortunatly and I would MUCH Appreciate a solution to this.

In short. How do I properly create an array of arrays 8x8 in size filled with pointers referencing their respective tile objects ?

Is this even possible or smart to do using memory like that?

mind that I did read lots of examples on this, and doing so with integers or other datatypes has been a succes. That however simply isn't want I want allocated

Tile.h here and Tile.cpp below

class Tile
{       
public:

private:
    enum TileOccupation
    {
        EmptyTile = 0,
        WhiteSphere = 1,
        BlackSphere = 2
    };

    unsigned short horizontalDimensions;
    unsigned short verticalDimensions;

public:
    Tile();
    Tile(unsigned short horizontalDimensions, unsigned short verticalDimensions);
    ~Tile();

void Update();
void Draw();


};

> Tile.cpp

#include "stdafx.h"
#include "Tile.h"

Tile::Tile()
{

}

Tile::Tile(unsigned short horizontalDimensions, unsigned short verticalDimensions)
{

}

void Tile::Update()
{

}

void Tile::Draw()
{

}


You don't want to create a Tile::Tile (a constructor), you want to create a Tile (an object) - change new Tile::Tile to new Tile.

Besides there is a leak. Remove this:

Grid::grid_ptr[i][0] = new Tile::Tile(10,10);

// EDIT

Probably you were confused with dynamic arrays (the one we use when dimensions are unknown). Your array is static (booth dimensions) and you don't have to dynamically allocate memory for it. It is part of your class and is created automatically with an object, the same way like e.g. simple int field.

And one more. Consider if you really have to create tiles dynamically. Will you move tiles in and out of the array? Is the Tile bigger then few int`s? If the answer is "no" for any question then static allocation will be fine for you:

Tile grid_ptr[8][8];
0

精彩评论

暂无评论...
验证码 换一张
取 消