2017-07-07 93 views
-2

我有問題,當我嘗試在FBullCowGame.h中更改我的2私有類變量。它似乎像構造函數調用函數Reset()[位於FBullCowGame.cpp]重置()函數不會改變整數MyMaxTries & MyCurrentTry.I'm新來C++所以可能它是明顯的東西,但我找不到它。當調用函數時私有類變量不會改變

這是main.cpp中

#include <iostream> 
#include <string> 
#include "FBullCowGame.h" 

using FText = std::string; 

void PrintIntro(); 
void PlayGame(); 

FText GetGuess(); 
FText PrintGuess(); 

FBullCowGame BCGame;//Dodeljujemo naziv u main-u FBullCowGame-u , takodje ako ima neki kod u constructoru on ga izvrsava pri ovaj deklaraciji 


bool AskToPlayAgain(); 


int main() 
{ 

    bool bPlayAgain = false; 
    do { 
     PrintIntro(); 
     PlayGame(); 
     bPlayAgain = AskToPlayAgain(); 
     } 
    while (bPlayAgain); 

     return 0; 
} 

void PrintIntro() 
{ 
    //Define constant var 
    constexpr int WORD_LENGHT = 6; 

    //Welcome to the player and asking the guess 
    std::cout << "Welcome to Bulls and Cows\n"; 
    std::cout << "Can you guess my " << WORD_LENGHT; 
    std::cout << " letter isogram word?\n"; 
} 

void PlayGame() 
{ 
    BCGame.Reset(); 
    int MaxTries = BCGame.GetMaxTries(); 
    //Looping for guesses 
    for (int i = 1; i <= MaxTries; i++) 
    { 
     FText Guess = GetGuess(); 
     //Repeat the guess back to them 
     std::cout << "Your guess is: " << Guess << std::endl; 
     std::cout << std::endl; 
    } 
    return; 
} 

FText GetGuess() 
{ 
    int CurrentTry = BCGame.GetCurrentTry(); 

    //Player enters their guess 
    std::cout << std::endl << "Try " << CurrentTry << ".What is your guess?\n"; 
    FText Guess = ""; 
    std::getline(std::cin, Guess); 

    return Guess; 
} 

bool AskToPlayAgain() 
{ 
    FText Response = ""; 
    std::cout << "Do you want to play again (y/n) ?" << std::endl; 
    std::getline(std::cin, Response); 

    return (Response[0] == 'y') || (Response[0] == 'Y'); 


} 

FBullCowGame.h/

#pragma once 
#include <string> 

class FBullCowGame { 
public: 
    FBullCowGame();//Constructor izvrsava se kod u njemu pri deklaraciji BCGame u nasem slucaju 


    int GetMaxTries() const; 
    int GetCurrentTry()const; 
    bool IsGameWon()const; 

    void Reset(); 
    bool CheckGuessValidity(std::string); 



private: 
    //Compile time values gets overwritten by run time values in Constructor 
    int MyMaxTries; 
    int MyCurrentTry; 



}; 

而且FBullCowGame.cpp/

#include "FBullCowGame.h" 

FBullCowGame::FBullCowGame() 
{ 
    //Run time values 
    Reset(); 
} 

void FBullCowGame::Reset() 
{ 

    constexpr int MAX_TRIES = 8; 
    int MyMaxTries = MAX_TRIES; 
    int MyCurrentTry = 1; 

    return; 
} 

int FBullCowGame::GetMaxTries()const 
{ 
    return MyMaxTries; 
} 

int FBullCowGame::GetCurrentTry()const 
{ 

    return MyCurrentTry; 
} 

bool FBullCowGame::IsGameWon()const 
{ 
    return false; 
} 

bool FBullCowGame::CheckGuessValidity(std::string) 
{ 
    return false; 
} 
+1

你'內重新聲明Reset'的變量:'INT MyMaxTries =超時參數; int MyCurrentTry = 1;'刪除'int'以避免重新聲明。投票結束,因爲印刷錯誤。 – AndyG

+1

重置中,您正在創建一個新變量,而不是重置舊變量。不要經常重新聲明變量。 – Carcigenicate

+1

當在'Reset()'方法中引用想要更改的變量時,請除去'int'。你正在經歷的叫做「陰影」。 –

回答

2

您正在使用功能局部變量遮蔽你的成員變量碰巧有相同的名字。

void FBullCowGame::Reset() 
{ 

    constexpr int MAX_TRIES = 8; 
    int MyMaxTries = MAX_TRIES; 
    int MyCurrentTry = 1; 

    return; 
} 

只是分配給您的成員變量,但不重複聲明他們

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