Passing Objects [C++]

Started by
11 comments, last by rip-off 12 years, 9 months ago
I want to pass my array of Tile objects in the set_Tiles function but i always seem to be getting an error. Here is my main function and the header file where the set_Tiles function is right under.


int main() {
//Tile object
Tile tiles[529];

//Set the current gamestate object
GameState* currentState = new Level_One( event, STATE_EXIT, camera, screen, nextState, LEVEL_WIDTH );

currentState->set_Tiles( tiles ); // error
}



#ifndef LEVELONE_H
#define LEVELONE_H

#include "GameState.h"

class Level_One : public GameState {
// Irrelevant things
public:
Level_One( SDL_Event, int, SDL_Rect, SDL_Surface*, int, int );
~Level_One();
void set_Tiles( Tile[] );
};

#endif


The error says this:

error C2061: syntax error : identifier 'Tile'


Can someone please tell me how to pass this array of objects correctly and how to pass objects in functions correctly?
Advertisement
Can someone please tell me how to pass this array of objects correctly and how to pass objects in functions correctly?

The argument to set_Tiles doesn't have a name. Try this:

[font="Courier New"]void set_Tiles( Tile [color="#FF0000"]tiles[] ); [/font]

[quote name='xx6heartless6xx' timestamp='1310435201' post='4834087']Can someone please tell me how to pass this array of objects correctly and how to pass objects in functions correctly?

The argument to set_Tiles doesn't have a name. Try this:

[font="Courier New"]void set_Tiles( Tile [color="#FF0000"]tiles[] ); [/font]
[/quote]

I tried that but it still doesnt work. And correct me if I'm wrong here but I believe you just need to put the object type in the function prototype and you dont need to put the name of the object.
I tried that but it still doesnt work. And correct me if I'm wrong here but I believe you just need to put the object type in the function prototype and you dont need to put the name of the object.


You are right :) Do you include "GameState.h" in the file with your main function?

[quote name='xx6heartless6xx' timestamp='1310438347' post='4834106']I tried that but it still doesnt work. And correct me if I'm wrong here but I believe you just need to put the object type in the function prototype and you dont need to put the name of the object.


You are right :) Do you include "GameState.h" in the file with your main function?
[/quote]

Yes it is included.
Yes it is included.

Ok. Try to read the description of the error message (it basically tells you that "Tile" is not recognized the place in your code where the error is generated):
http://msdn.microsoft.com/en-us/library/yha416c7%28v=vs.80%29.aspx
You are right :) Do you include "GameState.h" in the file with your main function?

Just realized that my intention was to ask you if "Tile.h" was included by the file with your main function. My bad.
I've been doing some researching and read about "forward declarations." Is this the solution?
I've been doing some researching and read about "forward declarations." Is this the solution?


No, you can only use forward declarations when your variable is a pointer, like

[font="Courier New"]class MyClass;

void main ( void)
{
MyClass* p; // OK
MyClass a[10]; // error
}

Why can't you just include the file in which you declare Tile?

FYI: I am going to bed now.
[/font]

[quote name='xx6heartless6xx' timestamp='1310441904' post='4834120']I've been doing some researching and read about "forward declarations." Is this the solution?


No, you can only use forward declarations when your variable is a pointer, like

[font="Courier New"]class MyClass;

void main ( void)
{
MyClass* p; // OK
MyClass a[10]; // error
}

Why can't you just include the file in which you declare Tile?

FYI: I am going to bed now.
[/font]
[/quote]

Alright thanks man ill keep working on it

This topic is closed to new replies.

Advertisement