2017-07-07 12 views
-2

FBullCowGame.hで2つのプライベートクラス変数を変更しようとすると問題が発生します。コンストラクタが関数Reset()[FBullCowGame.cppにあります。 Reset()関数は整数を変更しません。MyMaxTries & MyCurrentTry.Iは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

を再宣言していない:' int型MyMaxTries = MAX_TRIES。 int MyCurrentTry = 1; '再宣言を避けるために' int'を削除します。入力ミスとして閉じるように投票しています。 – AndyG

+1

Resetでは、古い変数をリセットせずに新しい変数を作成しています。変数を常に再宣言しないでください。 – Carcigenicate

+1

'Reset()'メソッドの中で変更したい変数を参照する際に 'int'を削除してください。あなたが経験していることは、「シャドーイング」と呼ばれています。 –

答えて

2

ですその名前はまったく同じです。

void FBullCowGame::Reset() 
{ 

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

    return; 
} 

ちょうどあなたのメンバ変数に代入していますが、 `Reset`以内にあなたの変数を再宣言している彼らに

void FBullCowGame::Reset() 
{  
    constexpr int MAX_TRIES = 8; 
    MyMaxTries = MAX_TRIES; 
    MyCurrentTry = 1; 
} 
関連する問題