在另一个类中调用一个类的成员 [英] Calling member of one class in another

查看:109
本文介绍了在另一个类中调用一个类的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于作业,我需要使用使用多个类的模具创建一个游戏。对于 Dice.cpp 我写了一个函数,只是得到一个从1到6的随机模具卷,然后我需要写一个单独的类,需要模具卷的值和使用它来确定在游戏中添加了什么,我在 player.cpp 中做了。我试着使用 #includeDice.h就像我为main.cpp,但仍然告诉我,我的 d.getRoll 未在其作用域中定义。 takeTurn函数是问题所在,我需要使它void并且没有参数。任何帮助将是巨大的。



player.h

  #ifndef PLAYER_H 
#define PLAYER_H
#include< iostream>
#include< string>
#include< cstdlib>
#include< time.h>
#includeDice.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

  #includeplayer.h


player :: player()
{
numLeg = 0;
numHead = 0;
numEye = 0;
numWing = 0;
numBody = 0;
numAntenna = 0;
cootieName =未定义;
playerName =未定义;
}
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();
您滚动了一个<辊< 。 << 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称为< cootieName<< has:< endl;
cout<< numLeg<< 腿<< endl;
cout<< numHead< 头部<< endl;
cout<< numEye< 眼睛<< endl;
cout<<编号< 翅膀<< endl;
cout<< numBody<< 身体<< endl;
cout<< numAntenna< 天线<< 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

  #includeDice.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>
#includeDice.h
#includeplayer.h

using namespace std;

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

cout<< 输入播放器名称<< endl;
p.setPlayerName();
cout<< 您的姓名是< p.getPlayerName()<< 。 << endl endl;
cout<< 创造一个Cootie的时间! << endl;
cout<< 请命名你的Cootie! << endl;
p.setCootieName();
cout<< 使用模具辊添加身体部位。 << endl;
cout<< 为了滚动模具,输入1< endl;
cout<< 退出,输入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(); 


For an assignment I'm required to create a game using die rolls that uses multiple classes. For Dice.cpp I i wrote a function that just gets a random die roll from 1 to 6, and then I need to write a separate class that takes the die roll's value and uses it to determine what piece is added in the game, which i did in player.cpp. I tried using #include "Dice.h" just like i would for main.cpp, but that still tells me that my d.getRoll() is not defined in its scope. The takeTurn function is where the problem is, and I'm required to make it void and have no parameters. Any help would be great.

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

EDIT: Here's the 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;
        }
    }
}

解决方案

You need to either create an instance of Dice to call the method on like this

Dice d;
roll = d.getRoll();

or you need to make getRoll static. In the function declaration in Dice.h you'd put

static int getRoll();

and you would use it like this

roll = Dice::getRoll();

这篇关于在另一个类中调用一个类的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆