Jump to content
Sign in to follow this  
amitk3553

Error:Array initialization in constructor

Recommended Posts

Hello

 

i have declared array as below, have to initialize as below in constructor.

 

 

struct hci_top : sc_module 
  

unsigned int const_matrix_arr[16];        

------

------

Constructor

hci_top(sc_module_name nm):sc_module(nm), host_hci_tar_socket("host_hci_tar_socket")
  {
    const_matrix_arr[16] = {2, 3, 1, 1, 1, 2, 3, 1, 1, 1, 2, 3, 3, 1, 1, 2};     // Initialization
 
  }
 
};
 
Error: 355: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x
../design/hci_top.cpp:355: error: cannot convert ‘<brace-enclosed initializer list>’ to ‘unsigned int’ in assignment
 
 
Regards
cam
 
 

Share this post


Link to post
Share on other sites
Cam,

 

struct hci_top : sc_module 

{

   unsigned int const_matrix_arr[16]; // Neither const nor static..


 


   hci_top(sc_module_name nm):sc_module(nm), host_hci_tar_socket("host_hci_tar_socket")

   {

      const_matrix_arr[16] = {2, 3, 1, 1, 1, 2, 3, 1, 1, 1, 2, 3, 3, 1, 1, 2};     // Initialization

      // Above is not initialization, it is accessing just one element of the array

   }

};

 

If the const_matrix_arr is NOT a const, you can initialize the elements individually:

const_matrix_arr[0] = 2;

const_matrix_arr[1] = 2;

/* Etc.. */

 

If the const_matrix_arr is really const, then you must declare it as const:

const unsigned int const_matrix_arr[16];

Then you might expect to initialize it in the constructor:

hci_top(...) : const_matrix_arr({2, 3, 1, 1, /* etc */})

{

   // Note: the initialization in not in the body of the ctor!

}

But this doesn't work in pre-C++0x compilers.

 

Since it doesn't make much sense to have a non-static const, if you declare the variable additionally as static, then you can initialize it:

const unsigned int hci_top::const_matrix_arr[16] = {2, 3, 1, 1, /* etc */};

 


Share this post


Link to post
Share on other sites

 

Hello

 

i have declared array as below, have to initialize as below in constructor.

 

 

struct hci_top : sc_module 
  

unsigned int const_matrix_arr[16];        

------

------

Constructor

hci_top(sc_module_name nm):sc_module(nm), host_hci_tar_socket("host_hci_tar_socket")
  {
    const_matrix_arr[16] = {2, 3, 1, 1, 1, 2, 3, 1, 1, 1, 2, 3, 3, 1, 1, 2};     // Initialization
 
  }
 
};
 
Error: 355: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x
../design/hci_top.cpp:355: error: cannot convert ‘<brace-enclosed initializer list>’ to ‘unsigned int’ in assignment
 
 
Regards
cam

 

Hello Sir,

Please check out a good reference on C++.

Unfortunately, what you are trying goes against C++ grammar rules

and no wonder the compiler barfs. However, there is a very simple

solution, a plain simple C style initialization method, that will always

work.

How about arr[0] = 1;

etc., etc.,

Note that  C++ is derived from the mother language C, so why not

use plain C,  when it suits you ? After all, your aim is to solve your

problem, and not bother with the details of the C++ language

grammar.

Share this post


Link to post
Share on other sites

Actually, Cam's code is almost correct with respect to the array if using C++11. If your version of GCC is relatively recent (e.g. 4.7) or perhaps you are using CLANG++ versions 3.4 or later, or even Visual Studio C++ 2013, then there is a new initializer syntax. That is what the error message was trying to tell you. In C++11, you can do things like:

int array_2[2] = {1,2}; // creates an array of 2 elements
std::vector<int> vector_1 = { 1, 2, 3, 4 }; // creates a vector of 4 elements
auto array_1[] = {1U, 2U, 3U}; // creates an array of 3 unsigned int elements
The following should work with GCC 4.7 if you include the -std=c++11 switch:
 
#include <array>

struct hci_top : sc_module 
{ 
  
std::array<unsigned int,16> const_matrix_arr;        
------
------
Constructor
hci_top(sc_module_name nm):sc_module(nm), host_hci_tar_socket("host_hci_tar_socket")
  {
    const_matrix_arr = {2, 3, 1, 1, 1, 2, 3, 1, 1, 1, 2, 3, 3, 1, 1, 2}; // Initialization
 
  }
 
};

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×