Array initializing error

I know that the error that I'm getting has something to do with it only being compatible with C++11 but is there any way I could work around that by adjusting my code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
srand(time(0));
	int randGame = rand() % 1 + 4;
	string lockedSlots[50];
	string sudoku[10][10] =
		{
		//      1    2    3    4    5   6   7   8   9
		{ " ", " ", " ", " ", " ", " "," "," "," "," "},//IGNORE ROW
		{ " ", " ", " ", " ", " ", " "," "," "," "," "},//1
		{ " ", " ", " ", " ", " ", " "," "," "," "," "},//2
		{ " ", " ", " ", " ", " ", " "," "," "," "," "},//3
		{ " ", " ", " ", " ", " ", " "," "," "," "," "},//4
		{ " ", " ", " ", " ", " ", " "," "," "," "," "},//5
		{ " ", " ", " ", " ", " ", " "," "," "," "," "},//6
		{ " ", " ", " ", " ", " ", " "," "," "," "," "},//7
		{ " ", " ", " ", " ", " ", " "," "," "," "," "},//8
		{ " ", " ", " ", " ", " ", " "," "," "," "," "} //9
		};



	if (randGame == 1)
	{
	sudoku =
	{
	//      1    2    3    4    5   6   7   8   9
	{ " ", " ", " ", " ", " ", " "," "," "," "," "},//IGNORE ROW
	{ " ", "4", "5", "8", "6", " "," ","2","3","1"},//1
	{ " ", "3", " ", " ", " ", " ","2","7"," "," "},//2
	{ " ", " ", "7", "2", "5", "1"," "," "," "," "},//3
	{ " ", " ", "6", "7", " ", "8"," ","3"," "," "},//4
	{ " ", " ", " ", "4", " ", "9"," ","5"," "," "},//5
	{ " ", " ", " ", "9", " ", "5"," ","6","8"," "},//6
	{ " ", " ", " ", " ", " ", "2","8","9","6"," "},//7
	{ " ", " ", " ", "5", "9", " "," "," "," ","3"},//8
	{ " ", "9", "4", "6", " ", " ","5","1","2","8"}// 9
	};


And the error that I receive is

1
2
- assigning to an array from an initializer list
- extended initializer lists only available with -std=c++0x or -std=gnu++0x [enabled by default]
Last edited on
Arrays are not Copyable or Assignable. Use std::vector<>?

And favour the C++ random number library over the C random number library.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <vector>
#include <string>
#include <random>
#include <ctime>

int main()
{
    const std::size_t NSLOTS = 50 ;
    std::vector<std::string> lockedSlots(NSLOTS);

    const std::size_t NSUDOKU = 10 ;
    // initialise with all " "
    std::vector< std::vector<std::string> > sudoku( NSUDOKU, { NSUDOKU, " " } ) ;

    std::mt19937 rng( std::time(nullptr) ) ;
    std::uniform_int_distribution<int> distrib( 1, 4 ) ;
    const int randGame = distrib(rng);

    if (randGame == 1)
    {
        sudoku =
        {
            //      1    2    3    4    5   6   7   8   9
            { " ", " ", " ", " ", " ", " "," "," "," "," "},//IGNORE ROW
            { " ", "4", "5", "8", "6", " "," ","2","3","1"},//1
            { " ", "3", " ", " ", " ", " ","2","7"," "," "},//2
            { " ", " ", "7", "2", "5", "1"," "," "," "," "},//3
            { " ", " ", "6", "7", " ", "8"," ","3"," "," "},//4
            { " ", " ", " ", "4", " ", "9"," ","5"," "," "},//5
            { " ", " ", " ", "9", " ", "5"," ","6","8"," "},//6
            { " ", " ", " ", " ", " ", "2","8","9","6"," "},//7
            { " ", " ", " ", "5", "9", " "," "," "," ","3"},//8
            { " ", "9", "4", "6", " ", " ","5","1","2","8"}// 9
        };
   }
}
I renamed NSUDOKU and NSLOTS to nSudoku and nLockedSlots but I got an error on this line.

vector <vector<string>> sudoku(nSudoku, {nSudoku, " "});

Here's the exact error message

a function call cannot appear in a constant-expression

Please help!
Seems to compile without errors on current versions of the mainstream implementations.
Which compiler (and version) are you using?
LLVM and GNU: http://coliru.stacked-crooked.com/a/c55e5da37d261431
Microsoft: http://rextester.com/AXKJ88434

If the compiler does not have full support for uniform initialisation, try:
1
2
// std::vector< std::vector<std::string> > sudoku( nSudoku, { nSudoku, " " } ) ;
std::vector< std::vector<std::string> > sudoku( nSudoku, std::vector<std::string>( nSudoku, " " ) ) ;
Your second suggestion worked.

I am using Linux with Eclipse IDE for C/C++ Developers Luna Service Release 2 (4.4.2).

Would you recommend a different compiler for a beginner?
Last edited on
You could update to a newer version of the same compiler.

GCC 5.1 is excellent - with a good compiler and (at long last) a good standard library implementation.
I'll check it out, thanks for the help and suggestions!
Topic archived. No new replies allowed.