Header file not loading

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2008
Posts: 203
Reputation: llemes4011 is an unknown quantity at this point 
Solved Threads: 13
llemes4011 llemes4011 is offline Offline
Posting Whiz in Training

Header file not loading

 
0
  #1
Jun 16th, 2009
Hi, I have another question. This one is about header files. I have a header for a player and the opponent of my battleship game. They way I have it set up, the opponent needs a player object to make a move, and vise versa. This is what i have.

Opponent Header:
  1. /*
  2.  * File: opponent.h
  3.  * Author: Neil
  4.  *
  5.  * Created on June 16, 2009, 4:25 PM
  6.  */
  7.  
  8. #ifndef _OPPONENT_H
  9. #define _OPPONENT_H
  10.  
  11. #include "ship.h"
  12. #include "field.h"
  13. #include "player.h"
  14. #include <vector>
  15. #include <iostream>
  16.  
  17. class opponent {
  18. public:
  19. opponent();
  20. opponent(const opponent& orig);
  21. virtual ~opponent();
  22.  
  23. void placeShips();
  24. void makeMove(player p);
  25. bool takeHit(int x, int y);
  26. void storeShips(std::vector<Ship> ships){this->ships=ships;}
  27. std::vector<Ship> getShips(){return ships;}
  28.  
  29. private:
  30. std::vector<Ship> ships;
  31. Field shipArea;
  32. Field guessArea;
  33. };
  34.  
  35. #endif /* _OPPONENT_H */

Player Header:
  1. /*
  2.  * File: player.h
  3.  * Author: Neil
  4.  *
  5.  * Created on June 16, 2009, 5:10 PM
  6.  */
  7.  
  8. #ifndef _PLAYER_H
  9. #define _PLAYER_H
  10.  
  11. #include "opponent.h"
  12. #include "ship.h"
  13. #include "field.h"
  14. #include <vector>
  15. #include <iostream>
  16.  
  17. class player {
  18. public:
  19. player();
  20. player(const player& orig);
  21. virtual ~player();
  22.  
  23. void placeShips();
  24. void makeMove(opponent o);
  25. bool takeHit(int x, int y);
  26. void printFields();
  27. void storeShips(std::vector<Ship> ships){this->ships=ships;}
  28. std::vector<Ship> getShips(){return ships;}
  29. void updateShipField();
  30. void updateGuessField(int x, int y);
  31.  
  32. private:
  33. std::vector<Ship> ships;
  34. Field shipArea;
  35. Field guessArea;
  36. };
  37.  
  38. #endif /* _PLAYER_H */

And this is the error I'm getting:
  1. In file included from opponent.h:13,
  2. from opponent.cpp:9:
  3. player.h:24: error: `opponent' has not been declared
  4. player.h:24: error: ISO C++ forbids declaration of `o' with no type
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Header file not loading

 
0
  #2
Jun 16th, 2009
First of all, get those #includes out of inside those #ifndef's

Your problem is your C file includes the opponent.h file first, which in turn includes player, before the opponent class is even defined.

As a work around include
class opponent;

Above the class player declaration.

This is typically used for cross-dependent header files!

But in implementation of classes, one should keep in mind dependendencies of classes, etc. If you need a cross-dependency, then typically your object data needs reorganization in a layering of dependencies. Like a stack of pancakes, dependencies are always upwards!
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 374
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Header file not loading

 
0
  #3
Jun 16th, 2009
What wildgoose gives as a solution is called a "forward declaration", so you can read up about it (where what why how etc.)

And yes, Pancake stacks are most of the time FILO ones. xD
Last edited by Clockowl; Jun 16th, 2009 at 9:13 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 203
Reputation: llemes4011 is an unknown quantity at this point 
Solved Threads: 13
llemes4011 llemes4011 is offline Offline
Posting Whiz in Training

Re: Header file not loading

 
0
  #4
Jun 16th, 2009
Thanks for the help =] I'm still new to C++, so the header files and such still kind of confuse me, but I think im starting to get the hang of it!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC