Issue with Classes sharing variables

Started by
7 comments, last by freelancemanga 17 years, 11 months ago
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]
Advertisement
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?
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
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
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.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
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.
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
- 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.
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...
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)

This topic is closed to new replies.

Advertisement