0

Trying to add value to vector :

std::vector<char[256] > readers;

char d[256];
strcpy(d, "AAA");
readers.push_back(d);

Got error:

an array cannot be initialized with a parenthesized initializer 

What I do wrong?

vico
  • 17,051
  • 45
  • 159
  • 315

2 Answers2

2

A C-style array is not assignable, so it cannot be used as the value type of a vector.

If you are using at least C++11, you can #include <array> and use std::array. (Historically available in Visual C++ 2008 SP1 as std::tr1::array).

#include <iostream>
#include <array>
#include <vector>
#include <cstring>


int main()
{
    std::vector< std::array<char, 256>> readers;

    std::array<char,256> d;
    strcpy(d.data(), "AAA");
    readers.push_back(d);

}
Bemwa Malak
  • 1,182
  • 1
  • 5
  • 18
0

You could use std::array instead of the antiquated raw arrays.

Like this:

#include <iostream>
#include <array>
#include <vector>
#include <cstring>


int main( )
{
    std::vector< std::array<char, 256> > readers;

    std::array<char, 256> d { "AAA" };
    // strcpy( d.data( ), "AAA" ); not required
    readers.push_back( d );

    std::cout << readers[0].data( ) << '\n';
}
digito_evo
  • 3,216
  • 2
  • 14
  • 42