给一个类访问另一个C ++

时间:2013-11-12 04:15:11

标签: c++ class scope

我正在尝试访问另一个类中的一个类的成员。我是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;
}

2 个答案:

答案 0 :(得分:3)

我想,这就是你要找的东西

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

working example (pointer->method syntax)

working example (reference.method syntax)

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

答案 1 :(得分:1)

让我来解决语法错误。

checkNumber()功能中:

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

这有两个问题:

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

  2. 除非是静态函数,否则不能在没有对象的情况下调用类的成员函数。此函数可以是静态的,因为它不使用成员变量(存在设计问题,但暂时忽略它们)。

  3. 同样在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;
    }