| | |
Header file not loading
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2008
Posts: 203
Reputation:
Solved Threads: 13
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:
Player Header:
And this is the error I'm getting:
Opponent Header:
C++ Syntax (Toggle Plain Text)
/* * File: opponent.h * Author: Neil * * Created on June 16, 2009, 4:25 PM */ #ifndef _OPPONENT_H #define _OPPONENT_H #include "ship.h" #include "field.h" #include "player.h" #include <vector> #include <iostream> class opponent { public: opponent(); opponent(const opponent& orig); virtual ~opponent(); void placeShips(); void makeMove(player p); bool takeHit(int x, int y); void storeShips(std::vector<Ship> ships){this->ships=ships;} std::vector<Ship> getShips(){return ships;} private: std::vector<Ship> ships; Field shipArea; Field guessArea; }; #endif /* _OPPONENT_H */
Player Header:
C++ Syntax (Toggle Plain Text)
/* * File: player.h * Author: Neil * * Created on June 16, 2009, 5:10 PM */ #ifndef _PLAYER_H #define _PLAYER_H #include "opponent.h" #include "ship.h" #include "field.h" #include <vector> #include <iostream> class player { public: player(); player(const player& orig); virtual ~player(); void placeShips(); void makeMove(opponent o); bool takeHit(int x, int y); void printFields(); void storeShips(std::vector<Ship> ships){this->ships=ships;} std::vector<Ship> getShips(){return ships;} void updateShipField(); void updateGuessField(int x, int y); private: std::vector<Ship> ships; Field shipArea; Field guessArea; }; #endif /* _PLAYER_H */
And this is the error I'm getting:
C++ Syntax (Toggle Plain Text)
In file included from opponent.h:13, from opponent.cpp:9: player.h:24: error: `opponent' has not been declared player.h:24: error: ISO C++ forbids declaration of `o' with no type
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!
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!
![]() |
Similar Threads
- Header file Generator (C)
- graphices header file (C)
- Header File (C++)
- header file/ classes (C++)
- compile header file (C++)
- Link source code and header file together? (C++)
- How to write a header file (C++)
Other Threads in the C++ Forum
- Previous Thread: Search a word, return the line
- Next Thread: trying to make a display function using level-order traversal
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





