1

I am trying to call a function on an object, but I'm getting this error. I have two classes: a Sudoku class and a Puzzle class. The Sudoku class inherits the Puzzle class. The Sudoku class has protected variable, a 2D array of Puzzle objects Puzzle libraryOfPuzzles[4][10]; When I try to call the setSolution function on a the libraryOfPuzzles[][]; , it doesn't work. However, when I call it on a Puzzle test[4][10]; that is created inside the function itself, it works. Why is this?

void Sudoku::createLibraries(string name) {
    Puzzle test[4][10]; //added for demonstration purposes
    string fileName = "";
    int i = 0;
    for (int k = 0; k < 40; k++) {
        fileName = name + to_string(k) + ".txt";
        string aWord;
        ifstream fin;
        fin.open(fileName);
        i = 0;
        int value = 0;
        while (!fin.eof()) {
            fin >> aWord;
            int value = stringToInt(aWord);
            libraryOfPuzzles[k / 10][k % 10].setSolution(i / 9, i % 9, value); //this line gives error
            test[k / 10][k % 10].setSolution(i / 9, i % 9, value); //this line is fine
            i++;
        }
    fin.close();
    }
}
Roy K.
  • 25
  • 5
  • 1
    Include the full error message in your question, and show how you declare `libraryOfPuzzles`. – super May 20 '18 at 20:42
  • https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – M.M May 20 '18 at 20:51

2 Answers2

0

createLibraries cannot be defined as static.

Ali Asadpoor
  • 327
  • 1
  • 13
  • This fixed it. Thank you! Why can't createLibraries be defined as static? – Roy K. May 20 '18 at 21:11
  • Because `libraryOfPuzzles` is a non-static member variable so to access it you need a valid `this` pointer, which static methods do not have. The error message from the compiler could be a little less cryptic but this is pretty elementary stuff. – Paul Sanders May 20 '18 at 23:34
0

I think you problem is that you array is not public. So libraryOfPuzzles[][]; can't access it but Puzzle test[4][10]; can.

Hopes this helps.