views:

106

answers:

5

EDITED BELOW FOR UPDATES!!! Thank you for the help, please by all means point out ALL mistakes , I don't care if it sounds rude or mean or whatever, emo junk. Just give it to me straight so I can understand what is going wrong.

Hi everyone!

I am a rookie C++ programmer trying to learn and gain some IRL exp for C++.

I am attempting the following inside my VC++ (vs2008) compiler:

typedef unsigned short USHORT;

class Grid  
{  
...
public:  
    Grid()  
    {  
        Tile[36]* tileList_ptr;
    }  
...
};

In essence, I want to put 36 tiles , slam them into an array nice and tidy on the heap for a 8x8 playfield that never changes in size. Like a chessboard. Refer to them with a pointer, and fiddle with them in the related cpp file if needed.

If you aren't laughing by now at this attempt then I probably made a syntax error instead of major design flaw :P

Any help would be much appreciated!

Thanks in advance

EDIT 24/08/2010 13:49 (Time of start)

My code is now as following example:

The Grid Header file Grid.h:

#include "Tile.h"

class Grid
{

//no more typedef used

public: 
    Tile grid[8][8];

private:    
    unsigned short selectedItemIndexValue;
public:
    Grid()
    {

        Initialize();       
    }
    ~Grid(){}

    void Update();
    void FinalizeMove(unsigned short index);
    void Draw();

private:
    void Initialize(); //Initializes members
};

The Grid.cpp file:

#include "stdafx.h"   
#include "Grid.h"   
//Not tile , that used to give me a class redefinition error

unsigned short selectedItemIndexValue;

//No more typedef used

void Grid::Update()
{
    //Respond to controller commands
}

void Grid::FinalizeMove(unsigned short index)
{

}

void Grid::Draw()
{

}

void Grid::Initialize()
{
    for(int i = 0; i < 4; i++)
    {
        Grid::grid[i] = new Tile::Tile(10,10);   // ATTEMPT AT FILLING ARRAY
    }
}

Tile.h file

class Tile
{       
public:

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

    unsigned short horizontalDimensions;
    unsigned short verticalDimensions;

public:
    Tile(){}    
    ~Tile(){}

void Update();
void Draw();

};

Tile.cpp file:

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

void Tile::Update()
{

}

void Tile::Draw()
{

}

The attempt at filling the array in Grid.cpp is returning via the compiler the following error message: "Error 1 error C2061: syntax error : identifier '{ctor}'"

MSDN helped me giving me this:

"Compiler Error C2061. The compiler found an identifier where it wasn't expected. Make sure that identifier is declared before you use it."

I have failed at analysing it's meaning. What exactly have I done wrong here ? And what other problems are there to be found in my project? All help and comments will be much appreciated. Just assume I know 0% of C++

A: 

Possibly you mean this:

Tile tileList_ptr[36];

or this:

Tile* tileList_ptr[36];

Alex Farber
I will try all and report back soon...busy busy :S
Proclyon
+2  A: 

If you want to allocate memory on the Heap, you have to use an allocation operator like "malloc" or "new" Your example would, if written correctly only increase the Constructors stack size.

Correct Version of the Stack Memory:

Tile tileList_ptr[36];

Correct Version of the Heap Memory:

Tile* tileList_ptr = new Tile[36];

However, if you use "new" or "malloc" you have to remember to use "delete" or "free" when you don't need the allocated memory anymore...

Tapdingo
ye I have allocation like that with new in my head using new or malloc, the thing is this is in a header file and, as far as I know about C++ ( which mind you is NOT much!) I have to do in the cpp file right? Assigning anything besides integral static const ( as my compiler reminds me ) in a class definition is illegal. Right?The code in the cpp file is this://Trying to fill 64 slots with tile objectstileList_ptr = new Tile[64]; Whilst the code in the header file is just showing it exists: //Trying to reserve 64 slotsTile[64]* tileList_ptr; and I need delete[] ye :)
Proclyon
+6  A: 

Hi, @user428435. Three things:

  1. 8x8 is 64, not 36
  2. What kind of problems are you having? If a compilation error, what is the error? Often careful reading of the errors can help you solve your problems. If the code compiles and runs, what does it do that you don't expect it to do?
  3. You probably meant

    Tile* tileList_ptr[36];

Blair Conrad
ye that is wrong, I was pretty tired when coding this and at the end of my line, if you can't do 8*8 anymore it's time to take a break alright!
Proclyon
Best advice so far Blair, is number 2, I am going to learn C~~~~ 1-4000 errors before I learn c++ it seems though ^^. A daunting challenge, but that's all the fun. Mostly the problem now is me learning syntax. Just getting a jagged array of objects atm is all that is left before the next step.
Proclyon
+1  A: 
typedef unsigned short Tile;  // <= assuming typo

class Grid  
{  
...
public:  
    Grid()  
    {
    }  
...
private: 
    Tile tileList[64]; // <= 8x8=64
};

If the number of tiles is fixed at compile-time, why not store an array of tiles, instead of an array of pointers to tiles?

sbi
Ye, and a strange one aswell, I Copy pasted code and my code does not have that tile there, so ignore that one.64, fixed. Tired. fail none-the-less.Because I was unaware of what choice would be best and somewhat on the way in understanding pointers VS arrays. I've been practicing some with pointers but it's not all there yet.The syntax is even a bump in the road for me at this learning stage, could you explain for me please if I want a single array that never changes in size holding 64 Tiles holding Either NOTHING A B what a recommended course of action could be ?
Proclyon
I'm having serious problems parsing your comment. Here's an answer to what I think you asked: If you want an array with a fixed length, you can use a simple array as shown in my code example. If you need to resize the array at run-time (or if it's size will only be known at run-time), you need `std::vector`.
sbi
+2  A: 

I am laughing. Your code, it's kind of full of fail :P

First tip: If you can avoid heap allocating, do. If your playfield is always 8x8, don't waste time with the heap (allocating the actual game class on the heap is another matter).

Second tip: Don't do that crappy typedef unsigned short USHORT thing. It's an unsigned short. Just use the type directly. USHORT is not going to change to not be an unsigned short. Plus, ALLCAPS names are reserved for macros. You probably picked this up from the Windows headers, they also use this. Really, it's a TERRIBLE idea.

Thirdly, 8x8 = 64, not 36, and you can just directly say 8x8.

What you want is something like this:

class Grid {
    Tile grid[8][8];
public:
    Grid() {
    }
    Tile* operator[](int index) {
        return grid[index];
    }
    const Tile* operator[](int index) const {
        return grid[index];
    }
    ...
};
DeadMG
I actually learned it in a C++ book big enough to crush a human skull, could you possibly link or inform me to more of these code conventions, like std:: is that juist plain annoying? I'd like to know before stepping on to many toes!Fixed 8x8 = 64, I blame fatigue, was a wrecking day, but ye fail nonetheless.Changing to multidimensional array will help with coordinates, I was aware of that, however I wanted to learn if I could save all my objects in arrays like I plan on doing at all first.Let me try this answer you gave, I will reply my findings a.s.a.p.Thanks :)
Proclyon
can't edit my own comment ? ah well... I think I need to replace multidimensional with jagged here as words.
Proclyon
@Proclyon: The grid[][] is an array. Plus, it's multidimensional, not jagged. Jagged arrays are multidimensional arrays where the lengths vary. As for std::, you should typically leave it in, it's a good idea. I don't actually have links to more code conventions, but here's a general statement: The WinAPI comes from C. Any conventions that it has are C conventions. Don't use those. Read the Windows header for how not to do it.
DeadMG
@Proclyon: See [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) for good C++ books. (If yours isn't listed there, it's very likely not considered a good one.) As for the `std::` prefix: read [this answer](http://stackoverflow.com/questions/2879555/c-stl-how-to-write-wrappers-for-cout-cerr-cin-and-endl/2880136#2880136) I gave a while ago.
sbi