-5

I need help, I want to call generateNumber from a class called MagicNumber in another class called GuessMachine.

MagicNumber class,

    int MagicNumber::generateNumber()
    {
        int dice = 1;   
        srand(time(0));
        dice = (rand() % 6 ) + 1;
        return dice;
    }

I want to call the above method in the class below, GuessMachine class.

    void GuessMachine::checkNumber(MagicNumber& mN, int a)
    {
        int guessNumber=0;
        int tries=0;

        while(nTimes>=tries)
        {
            cout<<"Please enter a number between 1 to 6: ";
            cin>>guessNumber;

            a.generateNumber(); //<-- This is where it went wrong

            tries++;
        }
    }

Please help.

4

3 回答 3

4

Of course, it should be

mN.generateNumber();

a is an int.

于 2012-10-11T14:20:16.627 回答
0

It should be mN.generateNumber();.

于 2012-10-11T14:20:53.743 回答
0

You need an instance of the MagicNumber class to call a member function. Use something like this:

int n = nM.generateNumber();
于 2012-10-11T14:21:36.383 回答