1

我正在尝试访问另一个班级中一个班级的成员。我对 C++ 相当陌生,所以如果这是一个简单的解决方法,请原谅我,但我找不到答案,所以我来到了这里。

在这种情况下,我想调用“init();” 来自 CGuessNumber 类和成员 CheckNumber。

这是我的代码。

#include <iostream>
#include <ctime>
#include <cstdlib>

class CGuessNumber
{
public:
    int GenerateNumber()
    {
        return rand() % 100 + 1;
    }

    void checkNumber(int guess, int answer, int &attempts)
{

    if (guess < answer)
    {
        std::cout << "TOO LOW, TRY AGAIN"  << "\n" << "TRYS LEFT: " << attempts  << "\n";
        attempts--;
    }else if(guess > answer)
    {
        std::cout << "TOO HIGH, TRY AGAIN"  << "\n" << "TRYS LEFT: " << attempts  << "\n";
        attempts--;
    }else if(guess ==  answer)
    {
        std::cout << "YOU WON!"  << "\n" << "TRYS LEFT: " << attempts  << "\n";
    }

    if (attempts <= 0)
    {
        std::cout << "YOU LOST!"  << "\n" << "TRYS LEFT: " << attempts  << "\n";
        CGAME::init(answer, attempts);
    }

}
}Number;

class CGAME
{
public:
        void init(int &answer, int &attempts)
    {
        answer = Number.GenerateNumber();
        attempts = 5;
    };

    int newGame()
    {
        srand (time(NULL));
        int intAnswer, playerGuess, trys;

        init(intAnswer, trys);

        while(intAnswer != playerGuess and trys > 0)
        {
            std::cin >> playerGuess;

            Number.checkNumber(playerGuess, intAnswer, trys);
        }
    };
}ONewGame;

int main()
{
    CGAME ONewGame
    ONewGame.newGame();

    return 0;
}
4

2 回答 2

3

我想,这就是你要找的

基本上,您可以将指向一个对象的指针传递给另一个对象的构造函数。在这种情况下,我们只需将一个指向 CGuessNumber 的指针传递给 CGAME 构造函数,我们还将这个指针存储在一个私有字段中以便我们可以使用它。然后你可以使用这个指针来调用方法。

工作示例(指针->方法语法)

工作示例(reference.method 语法)

#include <iostream>
#include <ctime>
#include <cstdlib>

class CGuessNumber
{
public:

    int GenerateNumber()
    {
        return rand() % 100 + 1;
    }

void checkNumber(int guess, int answer, int &attempts)
{

    if (guess < answer)
    {
        std::cout << "TOO LOW, TRY AGAIN"  << "\n" << "TRYS LEFT: " << attempts  << "\n";
        attempts--;
    }else if(guess > answer)
    {
        std::cout << "TOO HIGH, TRY AGAIN"  << "\n" << "TRYS LEFT: " << attempts  << "\n";
        attempts--;
    }else if(guess ==  answer)
    {
        std::cout << "YOU WON!"  << "\n" << "TRYS LEFT: " << attempts  << "\n";
    }

    if (attempts <= 0)
    {
        std::cout << "YOU LOST!"  << "\n" << "TRYS LEFT: " << attempts  << "\n";
    }
}
};

class CGAME
{
public:

    CGAME(CGuessNumber* pNumber)
    {
        m_number = pNumber;
    }
    void init(int &answer, int &attempts)
    {
        answer = m_number->GenerateNumber();
        attempts = 5;
    };

    void newGame()
    {
        srand (time(NULL));
        int intAnswer, playerGuess, trys;

        init(intAnswer, trys);

        while(intAnswer != playerGuess and trys > 0)
        {
            std::cin >> playerGuess;

            m_number->checkNumber(playerGuess, intAnswer, trys);
        }
    };

    private:
        CGuessNumber* m_number;
};

int main()
{
    CGuessNumber* pGnum = new CGuessNumber();
    CGAME* ONewGame = new CGAME(pGnum);
    ONewGame->newGame();
    return 0;
}
于 2013-11-12T04:47:10.947 回答
1

让我来解决语法错误。

checkNumber()函数中:

...
CGAME::init(answer, attempts);
...

这有两个问题:

  1. CGAME 尚未声明,因此编译器不知道它的存在,也不知道它是什么。为了避免这种情况,通常所有类都在顶部(或头文件)声明,所有函数都在后面定义。

  2. 你不能调用没有对象的类的成员函数,除非它是一个静态函数。这个函数可以是静态的,因为它不使用成员变量(存在设计问题,但现在让我们忽略它们)。

同样在 main() 你错过了一个';',但我想你已经知道了:-)

因此,应用这些更改:

#include <iostream>
#include <ctime>
#include <cstdlib>

// only declaring the classes here
class CGAME
{
public:
    static void init(int &answer, int &attempts);
    int newGame();
}ONewGame;

class CGuessNumber
{
public:
    int GenerateNumber();
    void checkNumber(int guess, int answer, int &attempts);
}Number;

// defining all the class member functions now
int CGAME::newGame()
{
    srand (time(NULL));
    int intAnswer, playerGuess, trys;

    init(intAnswer, trys);

    while(intAnswer != playerGuess and trys > 0)
    {
        std::cin >> playerGuess;

        Number.checkNumber(playerGuess, intAnswer, trys);
    }
}

int CGuessNumber::GenerateNumber()
{
    return rand() % 100 + 1;
}

void CGuessNumber::checkNumber(int guess, int answer, int &attempts)
{
    if (guess < answer)
    {
        std::cout << "TOO LOW, TRY AGAIN"  << "\n" << "TRYS LEFT: " << attempts  << "\n";
        attempts--;
    }else if(guess > answer)
    {
        std::cout << "TOO HIGH, TRY AGAIN"  << "\n" << "TRYS LEFT: " << attempts  << "\n";
        attempts--;
    }else if(guess ==  answer)
    {
        std::cout << "YOU WON!"  << "\n" << "TRYS LEFT: " << attempts  << "\n";
    }

    if (attempts <= 0)
    {
        std::cout << "YOU LOST!"  << "\n" << "TRYS LEFT: " << attempts  << "\n";
        CGAME::init(answer, attempts);
    }
}

void CGAME::init(int &answer, int &attempts)
{
    answer = Number.GenerateNumber();
    attempts = 5;
}

int main()
{
    CGAME ONewGame;
    ONewGame.newGame();

    return 0;
}
于 2013-11-12T06:21:03.780 回答