0

I've seen a number of posts on this but they all seem to be addressing when spme sort of method is defined.

Background for app: Just trying to make a basic Sudoku game to get the hang of C++.

The error seems independent of the main function .cpp file, so I'll ignore it unless it's requested to keep the explanation short.

The board.h file:

#pragma once
class board
{
public:
    board(int gameSize, int diffifuclty) : gameSize(gameSize), difficulty(difficulty) {};
    ~board();

private:
    int gameSize; int difficulty;
    int game[gameSize][gameSize][gameSize][gameSize];
    void createRandom(); // Creates a random workable board.
    void hasSolution(); // Checks if there's a solution from the current state.

};

I haven't toyed much with the board.cpp file yet, as I was just busy defining everything in the board.h file to plan out what functions I want to write.

Anyways, I want to have a game board with gameSize and difficulty being inputted in the console. I'm getting the error mentioned in my title when I try to construct the multidimensional array for the game board. (So with Sudoku, the 9x9 game has game size 3 here.)

I'm not sure what the error is or how to make this array an attribute (I'm not sure if this is C++ terminology, so sorry) of board?

Osama Kawish
  • 292
  • 3
  • 12
  • Your array cannot use member variables for the size. they must be available at compile time. So either use a vector>>> or use a vector of gamesize^4 with a translation function, use a int**** or pass it as a template parameter. – Beached Oct 14 '17 at 04:11

1 Answers1

0

The problem you have is a typical OOP problem with C++. You can find more explanation here .

It is because you did not create an object first before you refer to any member of the class.

For example,

construct(game); // game is a member of class board. you need to create an object of board first.

Here is the correct one

board bd;
construct(bd.game);
CS Pei
  • 10,211
  • 1
  • 23
  • 41
  • Ok so this is where I get confused, as I'm guessing C++ is different than other languages in this aspect. First off, does this code go into my `main`, board.h file, or board.cpp file? Second, how do I make a multidimensional array as required in my class then? In other languages, doing something similar wouldn't give me a problem, as I'm just using an attribute. So what's different here? – Osama Kawish Oct 14 '17 at 02:23
  • as far as I know, Java, C#, etc have the same concept regarding `static`. C++ is not different from them. normally board.cpp is the implementation of class board. the instantiation of board class is done somewhere else. – CS Pei Oct 14 '17 at 02:28