c++ - 使用前重置成员变量

标签 c++

我正在学习 udemy.com 上名为“虚幻引擎开发人员类(class)”的教程,我被卡在了 C++ 部分的某个部分。

我创建了一个带有构造函数的对象来初始化我的变量,这是有效的,当我在构造函数运行时打印变量时,我得到了预期的行为,但是当我在程序中使用 getter 输出变量时,值总是0. 请帮忙 =)

FBullCow.cpp

#include "FBullCow.h"
FBullCow::FBullCow()
{
    Reset();
}
void FBullCow::Reset()
{
    constexpr int MAX_TRIES = 8;
    int MyCurrentTry = 1;
    int MyMaxTries = MAX_TRIES;
    return;
}
int FBullCow::GetMaxTries() const
{
    return MyMaxTries;
}
int FBullCow::GetCurrentTry() const
{
    return MyCurrentTry;
}
bool FBullCow::IsGameWon() const
{
    return false;
}
bool FBullCow::CheckGuessValidity(std::string) const
{
    return false;
}

FBullCow.h

#pragma once
#include <string>
class FBullCow
{
public:
    FBullCow();
    void Reset();
    int GetMaxTries() const;
    int GetCurrentTry() const;
    bool IsGameWon() const;
    bool CheckGuessValidity(std::string) const;
private:
    int MyCurrentTry;
    int MyMaxTries;
};

主要.cpp

#include <iostream>
#include <string>
#include "FBullCow.h"
void intro();
std::string GetGuess();
void PlayGame();
bool AskToPlayAgain();
FBullCow BCGame;
int main()
{
    //Introduce the game
    intro();
    do
    {
        //Play the game
        PlayGame();
    }
    while (AskToPlayAgain() == true);
    return 0;
}
void intro ()
{
    //Introduce the game
    constexpr int WORD_LENGTH = 5;
    std::cout << "Welcome to my bull cow game\n";
    std::cout << "Can you gues the " << WORD_LENGTH << " letter isogram I'm thinking of?\n";
    return;
}
std::string GetGuess()
{
    std::string Guess = "";
    std::cout << "You are on try " << BCGame.GetCurrentTry() << std::endl;
    std::cout << "Enter your guess: ";
    std::getline(std::cin, Guess);
    return Guess;
}
void PlayGame()
{
    std::cout << BCGame.GetMaxTries() << std::endl;
    std::cout << BCGame.GetCurrentTry() << std::endl;
    int MaxTries = BCGame.GetMaxTries();
    //Loop for the number of turns asking for guesses
    for(int i = 0; i < MaxTries; i++)
    {
        std::string Guess = GetGuess();
        //Get a guess from the player
        //Repeat guess back to them
        std::cout << "Your guess was " << Guess << std::endl;
    }
}
bool AskToPlayAgain()
{
    std::cout << "Do you want to play again? Y or N: ";
    std::string response = "";
    std::getline(std::cin, response);
    if(response[0] == 'y' || response[0] == 'Y')
    {
        return true;
    }
    return false;
}

最佳答案

在方法中:

void FBullCow::Reset()
{
    constexpr int MAX_TRIES = 8;
    int MyCurrentTry = 1;
    int MyMaxTries = MAX_TRIES;
    return;
}

这里设置的是局部变量,不是成员变量。只需删除 int 部分:

void FBullCow::Reset()
{
    constexpr int MAX_TRIES = 8;
    MyCurrentTry = 1;
    MyMaxTries = MAX_TRIES;
    return;
}

现在应该可以了。请注意,您的编译器应该警告您变量正在初始化但未使用。

关于c++ - 使用前重置成员变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43359668/

相关文章:

c++ - libtorrent-rasterbar7 : g++ linker unable to find libtorrent/session. hpp

c++ - 如何剖析优化代码并加速循环

c++ - 继承 : Selecting which base class methods to inherit

c++ - 如何使用独立于索引的函数初始化 vector ?

c++ - 无法在 OpenCL 和 C++ 中显示 <CL_DEVICE_MAX_WORK_ITEM_SIZE>

先前声明的 C++ 函数

c++ - 使用某些 OpenCV 函数时出现链接错误 "threshold"Eclipse

c++ - 使用 boost::bind 将自由函数 native 回调替换为成员函数

c++ - 原子会遭受虚假存储吗?

python - 将 boost::python::numpy::ndarray 作为(默认或非默认)参数传递给 boost::python 函数?