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

1

構文エラーに対処しましょう。

checkNumber()関数内:

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

これには 2 つの問題があります。

  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 に答える