0

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

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by mike_2000_17
1

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.

Edited by mike_2000_17: n/a

Votes + Comments
well said.
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.