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?
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?
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);
}
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';
}