试图在另一个类中访问一个类的成员函数

时间:2013-12-07 11:31:02

标签: c++ class

对于作业,我需要使用使用多个类的掷骰来创建游戏。对于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;
        }
    }
}

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

1 个答案:

答案 0 :(得分:1)

Ooookay让我们看看

当你在你的(无尽的顺便说一句)中调用p.takeTurn时,你在该函数内调用“d.getRoll()” - 这是没有编译的,因为在方法takeTurn中没有名为d的类骰子对象

是的,你在main中创建了一个Object Dice d(1,6),但takeTurn()并不知道。

你要做的是将创建的骰子对象(在main中)传递给函数takeTurn作为参数(使用&amp;所以你不创建副本)。

因为我不能让它留在那里: 你的函数takeTurn有这么大的结构,有很多“if”和“if else”。 尝试使用switch-case!

ps:你的while循环永远不会结束(不知道是否有意)