943,513 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 573
  • C++ RSS
Jun 16th, 2009
0

Header file not loading

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 41
Solved Threads: 13
Posting Whiz in Training
llemes4011 is offline Offline
224 posts
since Aug 2008
Jun 16th, 2009
0

Re: Header file not loading

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!
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jun 16th, 2009
0

Re: Header file not loading

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.
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008
Jun 16th, 2009
0

Re: Header file not loading

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!
Reputation Points: 41
Solved Threads: 13
Posting Whiz in Training
llemes4011 is offline Offline
224 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Search a word, return the line
Next Thread in C++ Forum Timeline: trying to make a display function using level-order traversal





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC