0

我的问题是,我想每次

Player

被创建,为其添加分配给该玩家的 10 个 Monster 类的实例。

到目前为止我的工作..

MAIN.CPP //让用户添加一个播放器。

if (myMenuChoice == 1) // add a player
      {
          if (numberOfPlayers>4)
          { 
              cout<<"Max of 5 players!"<<endl;
          }else{
              player[(numberOfPlayers)].addPlayer((numberOfPlayers));//create player class  
              numberOfPlayers++;

}

播放器.CPP

#include "main.h"

    Monster myMonsters[10]; //up to ten monsters on a team

Player::Player()
{

}

void Player::addPlayer(int x)
 {
    cout<<"added player"<<x<<endl;
    this->karma=0;
 }

所以我的想法是,当添加一个玩家时,它应该创建 10 个 myMonsters 对吗?还是我说错了?因此,如果我创建 player[1] ,我现在应该让 player1.myMonster[0] throw myMonster[9] 这与 player2.myMonster[0] throw myMonster[9] 的值不同

现在,当我为一个玩家列出 myMonsters 时,它会给出奇怪的输出,就像我选择了 player1 怪物一样

1
4
2

我选择了player2怪物

5

按这个顺序,当我列出 player1 的怪物时,我得到

5
4
2

似乎它只制作一个 myMonsters[10] 而不是每个班级,我该如何解决这个问题?

不确定您是否需要如何添加怪物或查看它们,但在这里,

在 main.ccp

player[x].addMonster(monsterSelection);//add monster to this player class

播放器.cpp

void Player::addMonster(int monstername)
{
    int x=0;  //first monster
    if(this->numberOfMonsters >0)//do they have monsters all ready?
        x=this->numberOfMonsters;
    else
        this->numberOfMonsters = x;
    if (x<10)   
    {
        myMonsters[x].getNewMonster(monstername);
        this->numberOfMonsters++;
        cout<<"current number of monsters is: "<<numberOfMonsters;
    }
    else
        cout<<"sorry no more monsters for you!! 10 max"<<endl;
}

列出怪物

player[x].listMyMonsters();

在播放器.cpp

void Player::listMyMonsters()
{
    int x;
    x = this->numberOfMonsters;
    cout<<"Total number of monsters: "<<numberOfMonsters<<endl;
    int monsterCount=0;
    while(x!=0)
    {   
        cout<<"Monster ";
        cout<< monsterCount ;
        cout<<":\t";
        cout<<myMonsters[monsterCount].name;
        monsterCount++;
        cout<<"\n";
        x--;
    }
}

更新


if i add myMonsters in player.h class errors i get.
1>c:\documents and settings\tngmorse\desktop\test monsters\monstertest\monstertest\player.h(13) : error C2146: syntax error : missing ';' before identifier 'myMonsters'
1>c:\documents and settings\tngmorse\desktop\test monsters\monstertest\monstertest\player.h(13) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\documents and settings\tngmorse\desktop\test monsters\monstertest\monstertest\player.h(13) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

播放器.h

    enum Race {none,Living, Dead, Nature};
class Player
{
    public:
     Player();
    ~Player(){}


    int numberOfMonsters;
    int karma;
    bool myTurn;
    Race myRace;
    Monster myMonsters[10]; //up to ten monsters on a team


    void addPlayer(int x);
    void addMonster(int monstername);
    void listMyMonsters();
    Race getRace() const { return myRace; }


};
4

1 回答 1

1

正如@Patashu 指出的那样。您在声明Monster myMonsters[10];之外声明了 for class Player,因此它不是每个实例的变量,它在整个程序中存在一次

这是制作Monster的成员变量的方法Player。下面的代码将编译。添加您需要的任何其他语句,添加到代码中

//播放器.h

#ifndef PLAYER
#define PLAYER
#include "Monster.h"
class Player
{
      Monster M[10];
};
#endif

//怪物.h

#ifndef MONSTER
#define MONSTER

class Monster
{

};

#endif

//main.h

#ifndef MAIN
#define MAIN

#include "Player.h"
#include "Monster.h"

#endif

//main.cpp

#include "Main.h"

int main()
{
    Player P;
    return 0;
}
于 2013-06-26T06:55:37.020 回答