classes over multiple files

I have 3 header files in my project and 3 .cpp files:
main.cpp, game.cpp, player.cpp
defines.h, game.h, and player.h
======================================================
In defines.h there are defined constants example:
1
2
3
4
5
6
#include <iostream>
#include <windows.h>
//inside define.h

#define     CIREASA         'C'
#define     STEA            'S' 

In player.h there is the Player class declaration..
In player.cpp there is the Player class implementation.

In game.h there is the Game class declaration
In game.cpp the Game class functions are implemented.

The game.cpp requires player.cpp and there is some error at linking
and i don't know why.. something about multiple definitions..?

Here is the error code generated:
==================================
Linking console executable: bin\Debug\SizzlingHot.exe

obj\Debug\player.o: In function `_ZN6PlayerD1Ev':
player.cpp:(.bss+0x0): multiple definition of `_player'

/main.cpp:(.bss+0x18): first defined here
obj\Debug\player.o: In function `_ZN6PlayerD1Ev':

/player.cpp:(.bss+0xc): multiple definition of `_game'

/main.cpp:(.bss+0x0): first defined here

game.o: In function `_ZSt3minIjERKT_S2_S2_':
/game.cpp:(.bss+0x0): multiple definition of `_game'

and a few more..just like this..

Here some more info that might help:
===========================
1
2
3
4
5
//inside main.cpp
#include "defines.h"
#include "game.h"
#include "player.h"

1
2
3
4
5
6
7
8
9
10
11
//inside game.h
#ifndef _game_h
#define _game_h

class Game {
    public:
        void print(int);                                    //print the board and fruits
        bool randomChance (unsigned int chance);            //randomChance
        void initBoard();                                   //initialzes the board with fruits
//...code continues....
#endif 

1
2
3
4
5
6
7
8
9
10
11
12
//inside defines.h
#ifndef _defines_h
#define _defines_h

#include <iostream>
#include <windows.h>

#define     CIREASA         'C'
#define     STEA            'S'
#define     SAPTE           '7'
#define     LAMAIE          'l'
//...code continues .. only #define CONSTANT stuff 

1
2
3
4
5
6
7
8
9
10
11
//inside player.h
#ifndef _player_h
#define _player_h

class Player {
    public:
        bool isOutOfMoney();                                 //check if player is out of money
        void increaseBet(int);                               //increases player's bet
        void decreaseBet(int);                               //decreases player's bet
//...code continues...
#endif 


Also probably really important.. and probably the problem:
============================================
1
2
3
4
5
//inside game.cpp
#include "game.h"
#include "player.h"
#include "defines.h"
/*  FUNCTION IMPLEMENTATION  */

1
2
3
4
5
//inside player.cpp
#include "player.h"
#include "game.h"
#include "defines.h"
/*  FUNCTION IMPLEMENTATION  */

I'm not sure how to make it work... the .cpp files need all those headers

So what is the problem ? I can't really tell.. it's the first project
in which I separated the code into multiple files
I can't spot any problems from just the snippits you posted.

Are you using any global variables? Putting globals in headers is tricky (and globals are ill advised).

Based on the errors, it looks like your problem is with how "_player" and "_game" (which I'm assuming are Player and Game object) are defined. Can you show us how and where those objects are defined?
I'm assuming that there is a #endif at the bottom of defines.h.
Last edited on
sadly the code is too long to post ... but I have uploaded the files here: http://www.upload4free.info/4VCCV0G21JB6/SizzlingHot.rar.html

@Disch:
============
1
2
3
4
5
6
7
8
9
10
//inside game.h
#ifndef _game_h
#define _game_h

class Player;   
class Game {
    public:
        void print(int); 
/* ... */
}player;

1
2
3
4
5
6
7
8
9
10
//inside player.h

#ifndef _player_h
#define _player_h

class Game;
class Player {
    public:
/* ... */
}game;


note: that's how the code looked at the time i got the "_player" & "_game" errors .. since then i modified the code ... and got other errors(`player' was not declared in this scope and
`game' was not declared in this scope)
... the code in the link is the modified code.
Last edited on
Okay yeah. That's your problem.

 
class Player { } game;


is shorthand for:

1
2
class Player { };
Player game;


So it's doing 2 things:

1) Defining your Player class (good, this should be in the header)
2) Creating a global object called 'game' (bad, globals should not be in headers like this)

Game and Player should both look like this:

1
2
3
4
class Game  // or class Player
{
//...
};  // no variable name here 


I assume this is the change you made.. and now you're getting those other errors you mentioned ('game' not declared in this scope, etc).

It sounds like you already started writing your program assuming that you had these global objects. To change it now might require lots of rewriting / modification.

You could get this to work with globals.... I mean I could show you how to do it.... but I really don't want to encourage that. Globals are evil.

Give me a few mins to check your source and I'll see what I can come up with.


EDIT:

Okay this isn't as severe as I thought. Your basic problem is you have functions doing things they shouldn't be doing. Interestingly enough, you have 'player' and 'game' declared locally in main(). So before you had two different player and game objects. So even if you were able to compile, you wouldn't get the results you were expecting.

These errors all look like they could be remedied by passing the Game or Player object as a parameter to the function, rather than relying on globals.

Let's start with game.cpp:

- line 155 you are checking player.decision, but Game is unaware of any 'player' object because 'player' is local to main. SO. A possible solutions is to have checkWin take an additional parameter. Either:

a) an int called decision (for which you would pass player.decision)
or
b) a const Player& called player (for which you would pass player)

(solution a is preferable. Try to keep classes as ignorant about other classes as possible)

- Same problem with line 216. And you can solve it the same way. Rather than calling player.getBet() from that function, you can have that function take 'bet' as a 3rd parameter.


The errors in player.cpp can be solved the same way.
Last edited on
thanks ... you're amazing, I pretty much solved my problem ... it looks just a tad more ugly than before but it works :P
It actually probably looks better now.

Globals are what are "ugly".

;P
Topic archived. No new replies allowed.