在另一个类中调用一个类的成员

时间:2013-12-07 10:32:39

标签: c++ function class

对于作业,我需要使用使用多个类的掷骰来创建游戏。对于Dice.cpp我写了一个函数,它只是从1到6得到一个随机的掷骰子,然后我需要写一个单独的类来获取掷骰子的值并用它来确定游戏中添加了什么片段我在player.cpp做过。我尝试使用#include "Dice.h"就像我对main.cpp一样,但仍然告诉我我的d.getRoll()没有在其范围内定义。 takeTurn函数是问题所在,我需要使它无效且没有参数。任何帮助都会很棒。

player.h

#ifndef PLAYER_H
#define PLAYER_H
#include <iostream>
#include <string>
#include <cstdlib>
#include <time.h>
#include "Dice.h"

using namespace std;

class player
{
    public:
        player();
        void setPlayerName();
        string getPlayerName();
        void setCootieName();
        string getCootieName();
        void takeTurn();
    private:
        int numLeg, numHead, numEye, numWing, numBody, numAntenna;
        string cootieName;
        string playerName;
        int roll;


};

#endif // PLAYER_H

player.cpp

#include "player.h"


player::player()
{
    numLeg = 0;
    numHead = 0;
    numEye = 0;
    numWing = 0;
    numBody = 0;
    numAntenna = 0;
    cootieName = "Undefined";
    playerName = "Undefined";
}
void player::setPlayerName()
{
    getline(cin, playerName);
}
string player::getPlayerName()
{
    return(playerName);
}
void player::setCootieName()
{
    getline(cin, cootieName);
}
string player::getCootieName()
{
    return(cootieName);
}
void player::takeTurn()
{
    roll = d.getRoll();
    "You rolled a " << roll << "." << endl;
    if (roll == 1)
    {
        numBody++;
    }
    else if (roll == 2)
    {
        numHead++;
    }
    else if (roll == 3)
    {
        numLeg++;
    }
    else if (roll == 4)
    {
        numAntenna++;
    }
    else if (roll == 5)
    {
        numWing++;
    }
    else
    {
        numEye++;
    }
    cout << "Cootie called " << cootieName << " has: " << endl;
    cout << numLeg << " Leg(s)" << endl;
    cout << numHead << " Head(s)" << endl;
    cout << numEye << " Eye(s)" << endl;
    cout << numWing << " Wings(s)" << endl;
    cout << numBody << " Body(s)" << endl;
    cout << numAntenna << " Antenna(s)" << endl;
}

Dice.h

#ifndef DICE_H
#define DICE_H
#include <iostream>
#include <string>
#include <cstdlib>
#include <time.h>

using namespace std;

class Dice
{
    public:
        Dice(int, int);
        int getRoll();
    private:
        int rollTop, rollBot;
};

#endif // DICE_H

Dice.cpp

#include "Dice.h"

Dice::Dice(int bot, int top)
{
    rollBot = bot;
    rollTop = top;
}
int Dice::getRoll()
{
    return(rand() % rollTop + rollBot);
}

编辑:这是main.cpp

#include <iostream>
#include <string>
#include <cstdlib>
#include <time.h>
#include "Dice.h"
#include "player.h"

using namespace std;

int main()
{
    srand (time(NULL));
    int playerChoice;
    Dice d(1,6);
    player p;

    cout << "Enter player name" << endl;
    p.setPlayerName();
    cout << "Your name is " << p.getPlayerName() << "." << endl << endl;
    cout << "Time to create a Cootie!" << endl;
    cout << "Please name your Cootie!" << endl;
    p.setCootieName();
    cout << "Add body parts using die rolls." << endl;
    cout << "To roll the die, input 1" << endl;
    cout << "To exit, input 0" << endl;
    cin >> playerChoice;
    while(1)
    {
        if (playerChoice == 1)
        {
            p.takeTurn();
        }
        else
        {
            return 0;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您需要创建Dice的实例来像这样调用方法

Dice d;
roll = d.getRoll();

或者您需要将getRoll设为静态。在Dice.h中的函数声明中,你要放

static int getRoll();

你会像这样使用它

roll = Dice::getRoll();