2

Hi I have something like:

enum abd {a,b,c};

class d { 
  private: 
    abd tab[3][3]; 
  public: 
    d() { 
      tab[3][3] = { {a,a,a}, {a,a,a}, {a,a,a} }; 
    } 
}

and it yells:

error C2440: '=' : cannot convert from 'initializer-list' to 'abd' and IntelliSense: too many initializer values.

I'm probably blind, cause I cant find my mistake so please tell me whats wrong and how to repair it.

1
  • You're accessing the array out of bounds. Keep in mind built-in arrays are not assignable. Commented Aug 13, 2014 at 22:01

3 Answers 3

2

This thing { {a,a,a}, {a,a,a}, {a,a,a} } called initializer list because you can use it only at initialization of array.

abd tab[3][3] = { {a,a,a}, {a,a,a}, {a,a,a} };
Sign up to request clarification or add additional context in comments.

Comments

2
class d { 
  private: 
    abd tab[3][3]; 
  public: 
    d() : tab { {a,a,a}, {a,a,a}, {a,a,a}} {} 
};

Update

It seems that using

    d() : tab { {a,a,a}, {a,a,a}, {a,a,a}} {} 

is a problem in VS 2013. See the bug report.

Without support for that, the only choice I can think of is using nested for loops to initialize tab:

d()
{
   for ( int i = 0; i < 3; ++i )
      for ( int j = 0; j < 3; ++j )
         tab[i][j] = a;
} 

3 Comments

What does one do pre-C++11?
Problem is that this leads to error C2536: cannot specify explicit initializer for arrays.
@R Sahu thanks I choose Vlads answer for std::fill but you are right too(and with VS2013 being a problem too :) )
1

Arrays have no assignment operators. You could write instead

class d { 
  private: 
    abd tab[3][3] = { {a,a,a}, {a,a,a}, {a,a,a} }; 
  public: 
    d() { 
    } 
};

If the compiler does not support this feature then use some standard algorithm as for example std::fill to assign values to the elements of the array.

For example

#include <algorithm>

enum abd  { a, b, d };

    class d { 
      private: 
        abd tab[3][3]; 
      public: 
        d() {
            std::fill( reinterpret_cast<abd *>( tab ), 
                       reinterpret_cast<abd *>( tab ) + 9,
                       a );         
        } 
    };
i

3 Comments

What if in-class initialization isn't supported?
@0x499602D2 In this case you can use some standard algorithm as for example std::fill to set values of the array.
Problem is that this leads to error C2536: cannot specify explicit initializer for arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.