#Initialized Vector in Constructor, not maintaining updated values in functions

16 messages · Page 1 of 1 (latest)

river token
#
    vector<int> numList;
}

void Game::start(int n, int m) {
    srand((unsigned) time(NULL));
    for (int a = 0; a < n; a++) {
        int randomNum = 1 + (rand() % m);
        Game::numList.push_back(randomNum);
    }
}

int Game::checkGuess(vector<int> guessList) {
    vector<int> compareVector(guessList.size(), 0);
    int numCorrect = 0;
    //Compare guesses to random number list and update compareVector if correct
    //The compareVector will ensure numbers are compared correctly if there are duplicates
    for (int guessCounter = 0; guessCounter < guessList.size(); guessCounter++) {
        //Loop through the list
        for (int listCounter = 0; listCounter < Game::numList.size(); listCounter++) {
            //check if value has been guessed or not to prevent one number counting for duplicates
            if (compareVector.at(listCounter) == 0) {
                if (guessList.at(guessCounter) == Game::numList.at(listCounter)) {
                    numCorrect++;
                    compareVector.at(guessCounter) = 1;
                    break;
                }
                else {
                    listCounter++;
                }
            }
        }
        guessCounter++;
    }
    //Once all values are checked, return the number correct
    return numCorrect;

}

I create the class and default constructor creates the numList vector which works fine, but when I call other functions within that class, numList is again size zero after updating it in the start function. How do I get it to maintain the updated values.

severe badgeBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

cerulean shuttle
#

this part is declaring a local vector, which probably isn't what you want.

Game::Game() {
    vector<int> numList;
}

where is Game::numList declared? what does that code look like?

river token
cerulean shuttle
#
class Game{
public:
    //
private:
  static std::vector<int> numList{};
};
#

throughout the 'Game' class
i'm assuming by this part you mean you want it to be shared across instances of the Game objects

#

but if you don't want that, just remove the static

river token
#

I just want it to be usable on all functions of the same game object

cerulean shuttle
#

ok yeah then remove the static

#
class Game{
public:
    //
private:
  std::vector<int> numList{};
};
dusk pelican
#

numList needs to be a member of the class:

class Game {
private:
  std::vector<int> numList;
};

then the constructor could initialize members like this:

Game::Game() : numList {} {} // after : we initialize each member of the class in order of declaration
#

the {} is an empty body for the 2nd snippet

cerulean shuttle
#

then you just access it anywhere in member functions like this:

Game::SomeMemberFunc() {
    numList.push_back(1);
    // or
    this->numList.push_back(1);
}
river token
#

Okay thanks, let me update a few things and check it out.

severe badgeBOT
#

@river token Has your question been resolved? If so, type !solved :)

river token
#

!solved