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

Calling member of one class in another

本文关键字:一个 成员 另一个 调用      更新时间:2023-10-16

对于一项任务,我需要使用骰子创建一个使用多个类的游戏。对于Dice.cpp,我写了一个函数,它只得到从1到6的随机骰子掷骰子,然后我需要写一个单独的类,它取骰子掷骰子的值,并用它来确定游戏中添加了什么棋子,就像我在player.cpp中做的那样。我试着像使用main.cpp一样使用#include "Dice.h",但这仍然告诉我我的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

播放器.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;
}

骰子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);
}

编辑:这是主.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;
        }
    }
}

您需要创建一个Dice的实例来调用类似的方法

Dice d;
roll = d.getRoll();

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

static int getRoll();

你会像这个一样使用它

roll = Dice::getRoll();