Hi!
I have a problem with a bit of code i'm writing.

What i want to do is:

In the header file:

private:
         int board[16];

And in the .cpp file constructor

ScoreBoard::ScoreBoard()
{

int board[16]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0};

}

the compiler doesn't complain but i still get trash numbers when i print the board.
How do i do to create the variable board correctly and then set it's values to those above?

Best wishes

Mats

In the constructor, you shouldn't have "int boar..", repeating the type (int) will declare a new variable called "board" that is local to the constructor. so you are initializing a local array board and never initializing the data member "board". So that is why the private data member "board" remains with garbage values.

This is allowed in the c++0x standard (add flag "-std=c++0x" to gcc to compile):

ScoreBoard::ScoreBoard() : board({-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0}) { };

Otherwise you have to initialize them one by one, i.e. with a loop from 0 to 14 to set to -1 and finally setting the last element to 0.

commented: well said. +31
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.