views:

78

answers:

2

Hi everyone! 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()
{

}
+2  A: 

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];
adf88
Ye I have not focussed on deleting it yet but I am aware of it thank you.Can you explain to me the difference then using this scope operator ?
Proclyon
moment please :)
adf88
Wow.... the error is gone....White space in the compiler error reporter. Thank you !Now for the learning aspect, what did I do wrong?(Yes cleaning the memory leak ofc)
Proclyon
`Tile::Tile` is a constructor. `new` operator does not expect constructor, it expects type of object you want to create (and the type is `Tile`). Constructor is always called automatically.
adf88
A: 

All done and done.

Thanks everyone :)

Proclyon