Sign in to follow this  

Issue with Classes sharing variables

This topic is 4248 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

Problem: A class contains a variable type of another class (in a different .h file). I'm getting three errors that don’t seem to make any sense at all. Can anyone help me on this? As the only solution I could think of, I've moved "Grid grid;" from player.h back to block.h, where class Grid is defined. It was the only way I could get it to work, but a lot of other unrelated redefinition issues arose (look at last post). ////////////////////////////// errors: error C2146: syntax error : missing ';' before identifier 'grid' and two of these: error C4430: missing type specifier - int assumed. Note: C++ does not support default-int All point to the same line in player.h (Grid grid;). ////////////////////////////// player.h #ifndef __PLAYER_H #define __PLAYER_H #include "main.h" #include "block.h" class Player { private: public: Grid grid; }; #endif ////////////////////////////// block.h #ifndef __BLOCK_H #define __BLOCK_H class Block { private: public: }; class Grid { private: public: }; #endif [Edited by - ronaldo on April 30, 2006 10:22:07 AM]

Share this post


Link to post
Share on other sites
I don't see any obvious goofs. Did you copy and paste some code and have #ifndef __BLOCK_H ... end up in another header, causing grid.h to not get included and Grid to not be defined?

Share this post


Link to post
Share on other sites
Quote:
Original post by smart_idiot
I don't see any obvious goofs. Did you copy and paste some code and have #ifndef __BLOCK_H ... end up in another header, causing grid.h to not get included and Grid to not be defined?


I checked and there are no other #ifndef __BLOCK_H at all.

I did try adding player.h and player.cpp to a previous version of the program that did not have them; it compiled without errors! The problem might be elsewhere...

It's going to be hell finding them. Any sugestions on how to zero in on it?

thanks for the help,
Ron

Share this post


Link to post
Share on other sites
I don't think the problem is with the code you posted.

Try a different identifier name than __BLOCK_H and see if that makes it work, because I suspect it's probably already defined for some strange reason. By the way, identifiers beginning with underscores are technically reserved for internal use by the compiler, so you aren't really supposed to use them.

Share this post


Link to post
Share on other sites
The major unknown here is main.h. player.h doesn't appear to need it anyway, so try not including that and see what happens.

PS. I'm moving this to General Programming as it's a more relevant forum for this.

Share this post


Link to post
Share on other sites
The general idea was supposed to be; Game contains Player, Player contains Grid, Grid contains Block (Tetris clone).

None of these would find the other for some reason, so in the end I separated everything save for Grid and Block (both within block.h, so there seems to be no problem there).

I might just be confused as to how variables are shared in-between included files. #ifndef is supposed to help eliminate redefinitions, right? I have #ifndef's in all my header files, and now 405 errors all related to redefinitions.

Is there anything I might have overlooked? can I use extern to resolve this?


Thanks for all the help,
Ron

Share this post


Link to post
Share on other sites
- Did you read this? In full? Again?

- Get rid of those underscores already.

- Have you considered that the problem might be in main.h instead?

- Do you need to do something with your compiler/IDE to tell it where to look for the other files?

- Try constructing a minimal, complete example (i.e. no implementations beyond what's needed to cause the problem) and posting that in full.

Share this post


Link to post
Share on other sites
Ignoring the problem seeing as I can offer nothing more than what has already been said, I'd like to point out one thing about your design. Does a Player really conatain a Grid? Wouldn't the Game contain the Grid and the Player? To me, this would be a more accurate representation of the how things work. If its going to be a huge pain to have the Grid be in Game, not Player then don't worry about it. If its no problem, then you might want to do it just to get some practice having using 'proper' OOP design. Of course, rules are ment to be broken...

Share this post


Link to post
Share on other sites
A game has players, each player has his own grid. Makes sense?

Thanks for the link Zahlman. Anyway, the main problem after I fixed up the linkage issues turned out to be fixed by a simple clean/rebuild solution command. I'll never understand why Visual C++ does this...

Thanks for all the help, the program compiles now, but doesn’t execute. I know there are still logic bugs, but these I can handle XD

p.s. never mind my username, I’m ronaldo, just forgot my old password, lol)

Share this post


Link to post
Share on other sites

This topic is 4248 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this