Error:assigning to an array from an initializer list

Please throw some light on following error.
1
2
3
unsigned int trans_val_1[4];
unsigned int xor_result_arr[4];
trans_val_1 = {xor_result_arr[0], xor_result_arr[1], xor_result_arr[2], xor_result_arr[3]};//Error:assigning to an array from an initializer list 


Error:assigning to an array from an initializer list
Last edited on
You probably want to do something like this:

1
2
	unsigned int xor_result_arr[4] = {0};  // initialise
	unsigned int trans_val_1[4] = {xor_result_arr[0], xor_result_arr[1], xor_result_arr[2], xor_result_arr[3]};
Yes, but there is an error:

Error:assigning to an array from an initializer list
You can't assign arrays like that, but you can if you instead use std::array.
1
2
3
4
5
6
7
8
9
10
11
12
#include <array>

int main()
{
	std::array<unsigned int, 4> trans_val_1;
	std::array<unsigned int, 4> xor_result_arr;
	
	trans_val_1 = {xor_result_arr[0], xor_result_arr[1], xor_result_arr[2], xor_result_arr[3]};

	// or simply use normal assignment
	trans_val_1 = xor_result_arr;
}
Yes, but there is an error:

Did you even try the code I posted?
Compiles fine for me.

But, as Peter said, std::array would be the way to go.
Last edited on
Does the OP says if it has a C++11 compiler ?
I had used like below, but its again showing:

Symbol 'array' could not be resolved

#include<array>

std::array<int, 4> arr_1;

Problem is just solved by using like below:

unsigned int xor_result_arr[4] = {xor_result_arr[0], xor_result_arr[1], xor_result_arr[2], xor_result_arr[3]};

but i had to do like
unsigned int xor_result_arr[4]; // here ( ={0}) is also not working
xor_result_arr[4] = {xor_result_arr[0], xor_result_arr[1], xor_result_arr[2], xor_result_arr[3]};

as its globally declared and assignment is at another place
Last edited on
Use a compiler that reasonably current.

And, if g++ or clang++, enable conformance with: -std=c++11 or -std=c++0x

http://coliru.stacked-crooked.com/a/01d5bb613695eac3
I had function definition like

void cal(unsigned int arr_1[]);

now i want to declare it using array syntax std::array like below

void cal(std::array<unsigned int, >);

Please correct it.
You'll need a template function :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <array>

template <std::size_t Size>
void cal( std::array<unsigned, Size>& arr )
{
    std::cout << "Size: " << arr.size() << std::endl;
}

int main()
{
    std::array<unsigned, 34> arr;
    cal( arr );
    std::array<unsigned, 100> arr2;
    cal( arr2 );
}


EDIT
Improved the code
Last edited on
> You'll need a template function

Yes.

And if we make it polymorphic on iterators we would get a great deal more flexibility.

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
#include <iostream>
#include <iterator>
#include <array>
#include <vector>
#include <deque>
#include <cmath>
#include <sstream>

template < typename RANDOM_ACCESS_ITERATOR >
void foo( RANDOM_ACCESS_ITERATOR begin, RANDOM_ACCESS_ITERATOR end )
{
    auto N = end - begin ;
    for( decltype(N) i = 0 ; i < N ; ++i ) begin[i] *= (i+1) ;

    for( ; begin != end ; ++begin ) std::cout << std::llround(*begin) << ' ' ;
    std::cout << '\n' ;
}

int main()
{
    double a[] = { -1.62, -1.49, -0.63, -0.33, 0, 0.33, 0.63, 1.49, 1.62 };
    foo( std::begin(a), std::end(a) ) ;

    std::array<double,9> b = { { -1.62, -1.49, -0.63, -0.33, 0, 0.33, 0.63, 1.49, 1.62 } };
    foo( std::begin(b), std::end(b) ) ;

    std::vector<double> c = { -1.62, -1.49, -0.63, -0.33, 0, 0.33, 0.63, 1.49, 1.62 };
    foo( std::begin(c), std::end(c) ) ;

    std::deque<double> d = { -1.62, -1.49, -0.63, -0.33, 0, 0.33, 0.63, 1.49, 1.62 };
    foo( std::begin(d), std::end(d) ) ;
}

http://coliru.stacked-crooked.com/a/33500a0fdec4930f
I had done like below:

template <std::size_t Size>

void sub_trans_out(std::array<unsigned int, size>& data_word);


but its showing errors:

error: ‘size’ was not declared in this scope
error: template argument 2 is invalid
it's capital 'S'
1
2
template <std::size_t Size>
void sub_trans_out( std::array<unsigned int, Size>& data_word );


btw size_t is a typedef for unsigned int
Last edited on
Topic archived. No new replies allowed.