Jump to content
  • Advertisement
Sign in to follow this  

Passing Objects [C++]

This topic is 2507 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

[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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

[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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

[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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!