This question already has an answer here:
Calling member of one class in another
                                
                                    (1个答案)
                                
                        
                                6年前关闭。
            
                    
对于一项作业,我需要使用使用多个类的骰子来创建游戏。对于Dice.cpp,我编写了一个函数,该函数从1到6随机获得一个骰子掷骰,然后我需要编写一个单独的类来获取骰子掷骰的值,并使用它来确定在游戏中添加了哪些棋子。我在player.cpp中做了。我尝试过使用#include "Dice.h"就像对main.cpp一样,但这仍然告诉我d.getRoll()不在其范围内。有人告诉我我不能将Dice d(1,6);放在main.cpp中,但是我不确定从这里到哪里。任何帮助都会很棒。

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


播放器

#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


播放器

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


骰子

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

最佳答案

哎呀,让我们看看

当您在(无限循环)while循环中调用p.takeTurn时,您会在该函数“ d.getRoll()”中进行调用-
那是没有编译的东西,因为在takeTurn方法中没有名为D的Class Dice对象

是的,您在主对象中创建了一个对象骰子d(1,6),但是takeTurn()对此并不了解。

您要做的是将创建的骰子对象(主要)传递给函数takeTurn作为参数(使用&,所以您不创建副本)。

因为我不能就这样让它呆在那里:
您的功能takeTurn具有一个很大的结构,其中包含许多“ if”和“ if else”。
尝试使用开关盒!

ps:同样,您的while循环永远不会结束(不知道是否打算这样做)

关于c++ - 尝试访问另一类中一个类的成员函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20440828/

10-11 03:34
查看更多