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:

/* 
 * 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:

/* 
 * 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:

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!

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 ;)

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.