amitk3553 Posted December 10, 2013 Report Posted December 10, 2013 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 Quote
karthickg Posted December 11, 2013 Report Posted December 11, 2013 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 */}; See: http://stackoverflow.com/questions/6036453/making-a-constant-array-in-c for reference. Quote
dakupoto Posted December 11, 2013 Report Posted December 11, 2013 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. Quote
David Black Posted January 7, 2014 Report Posted January 7, 2014 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 } }; Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.