调用另一个类中一个类的成员
Calling member of one class in another
对于一项任务,我需要使用骰子创建一个使用多个类的游戏。对于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();
相关文章:
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- C++ - 如何在结构向量中找到结构体一个成员的最大值?
- 无法将指向类的成员函数的函数指针作为参数传递给同一类的另一个成员函数
- 使用只有一个成员的工会的目的是什么?
- 访问从联合与另一个成员集复制的联合中的一个成员是否未定义或未指定?
- std::mutex作为一个成员变量对多个线程来说是安全的吗
- 使用线程从另一个成员函数调用一个Member函数
- 如何通过指针将模板成员函数传递给另一个成员函数
- 只有一个成员的匿名联盟
- 复制一个对象并使两者共享一个成员变量 (C++)
- 初始化指针或引用成员变量以指向另一个成员
- 在另一个成员函数中调用成员函数时'int'之前的预期主表达式
- C 将成员函数作为参数传递给另一个成员函数
- 将结构编写为二进制,其中一个成员是字符串
- 如何在另一个成员函数中修改具有常量返回类型的成员函数的返回值
- 从其后声明的另一个成员数据初始化成员数据是否为未定义行为
- 如何使一个成员变量等于在main()中设置的另一个成员变量
- 在另一个成员中动态添加类成员
- 在对结构数组的一个成员进行排序后,移动其成员的其余部分
- 将类成员函数作为参数从同一类的另一个成员函数传递