| | |
Header file not loading
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Aug 2008
Posts: 208
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
Views: 282 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





