Re: compile error: new : cannot specify initializer for arrays
wong_powah@yahoo.ca wrote:
[code]
#include <vector>
#include <iostream>
using std::cout;
using std::vector;
enum {DATASIZE = 20};
typedef unsigned char data_t[DATASIZE];
int setvalue(UInt8 *pValue)
{
for (int i = 0; i < DATASIZE; i++)
pValue[i] = '1' + i;
return 0;
}
void displayvalue(UInt8 *pValue)
{
for (int i = 0; i < DATASIZE; i++)
cout << pValue[i] << "\n";
}
int main(int argc, char* argv[])
{
enum {MAX_PROC = 5, MAX_OBJECT = 10000};
// I want to use vector instead of array because the array size is
determined by a variable
//data_t data[MAX_PROC][MAX_OBJECT];
vector<data_t> v[MAX_PROC] (10); // compile error here!
for (int i = 0; i < MAX_PROC; i++) {
setvalue(v[i][0]);
}
cout << "data\n";
for (i = 0; i < MAX_PROC; i++) {
displayvalue(v[i][0]);
}
return 0;
}
[/code]
The above program is a test program to test how to use vector instead
of array for my own project, so the setvalue and displayvalue function
is simplied for test purpose (they are actually functions from another
library which I cannot change).
I want to keep data_t defined as an array of unsigned char.
There is a compile error for the above program:
error: new : cannot specify initializer for arrays
Practically speaking, you're running into problems because C
style arrays are irremedally broken. In this case, the
obvious solution is to use a boost::array for your data_t,
and std::vector for all of the outer vectors.
At any rate, with the definition of data_t that you are
using, you can't have a vector<data_t> since data_t isn't
Assignable), and you cannot have a C style array of vector,
and expect to initialize them. On the other hand, something
like:
typedef boost::array< unsigned char, 10 > data_t ;
// ...
int
main()
{
data_t initialValue ;
setValue( initialValue ) ;
std::vector< std::vector< data_t > >
v( MAX_PROC,
std::vector< data_t >( MAX_OBJECT,
initialValue ) ) ;
// ...
}
Should work.
--
James Kanze (Gabi Software) email: james.kanze@gmail.com
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]