Как бы я использовал агрегацию с этим классом (код для начинающих)? - PullRequest
0 голосов
/ 18 мая 2019

Я хотел бы понять агрегацию, поэтому я попытался создать простой тестовый код, но он не работает в моем компиляторе, иногда он работает в Интернете.

код для основного:

#include <iostream>
#include <string>    
#include "player.h"
#include "game.h"
using namespace std;

int main(){ 
    game game;
    game.playGame();

    return 0;
}

код для game.cpp:

#include "game.h"
#include "player.h"

game::game(){
    player.setBalance(0); 
}

void game::playGame(){
  cout << "playing game." ;  //just for debugging
}

код для game.h:

#ifndef GAME  
#define GAME
#include <iostream>
using namespace std;

class game{

  private:
    player player;

  public:
    game();
    void playGame();

}; 


#endif

код для player.cpp:

#include "player.h"

 player::player(){
   balance = 0;
 }

 player::player(int theBalance){
   balance = theBalance;
 }

 int player::getBalance(){
   return balance;
 }

 void player::setBalance(int theBalance){
   balance = theBalance;
 }

код для player.h:

 #ifndef PLAYER // used on headers 
 #define PLAYER

 class player{

     private:
     int balance;

   public:

     player();
     player(int);
     int getBalance();
     void setBalance(int);
 }; 

 #endif

Я думаю, что проблема, вероятно, в заголовках. я получаю ошибку:

In file included from main.cpp:5:0:
game.h:10:12: error: declaration of 'player game::player' [-fpermissive]
 player player;

In file included from main.cpp:4:0:
player.h:5:7: error: changes meaning of 'player' from 'class player' [-fpermissive]
class player{
       ^~~~~~

1 Ответ

0 голосов
/ 18 мая 2019

Я думаю, что проблема в классовой игре, потому что он не знает классового игрока, использует предварительную декларацию или просто пишет #include "player.h" в game.h

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...