【问题标题】:Giving one class Access to another C++让一个类访问另一个 C++
【发布时间】:2013-11-24 02:30:07
【问题描述】:

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

【问题讨论】:

  • 这就是friend 的用途。
  • 这是一个很好的资源:@​​987654322@

标签: c++ class scope


【解决方案1】:

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

基本上,您可以将指向一个对象的指针传递给另一个对象的构造函数。在这种情况下,我们只需将一个指向 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;
}

【讨论】:

    【解决方案2】:

    让我来解决语法错误。

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

    【讨论】:

    • 如果这不适合在这里问,请告诉我,但是,您提到的设计问题是什么。我仍在学习,这是我正在努力学习的大部分内容。
    • 全局对象之一。类之间的紧密耦合。而且这些类并没有真正使用封装。 Andrew 的实现(另一个答案)实际上非常好,解决了很多问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-18
    • 2012-08-24
    相关资源
    最近更新 更多