compile error: new : cannot specify initializer for arrays

W

wong_powah

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;
}

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

How to fix this compile error?
Thanks in advance.
 
V

Victor Bazarov

Code:
#include <vector>
#include <iostream>

using std::cout;
using std::vector;

enum {DATASIZE = 20};
typedef unsigned char data_t[DATASIZE];[/QUOTE]

So, 'data_t' is an array.  It has no assingment semantics, you
realize that, yes?  Further, it has no copy-construction
semantics, which are actually required if you intend to place
your object into a standard container.  That means you cannot
use 'data_t' in 'vector', 'list', etc.
[QUOTE]
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![/QUOTE]

Well, of course there is an error.  What are you trying to declare
here?  An array of vectors?  Then what are the parentheses doing
there?  A vector of 'data_t'?  Then what are the brackets doing
there?
[QUOTE]
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;
}

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.

You cannot, most likely.
There is a compile error for the above program:
error: new : cannot specify initializer for arrays

How to fix this compile error?

By learning the language, I suppose. If you intend to declare 'v'
as a single vector, and then initialise it with 10 elements of type
'data_t' (which is likely impossible anyway), then you should do

vector<data_t> v(10);

If you intend to declare an array of 10 vectors, you do

vector<data_t> v[10];

What book on C++ are you reading that doesn't explain the concepts
of declaring an array versus using the standard 'vector' template?

V
 
J

James Kanze

Code:
#include <vector>
#include <iostream>[/QUOTE]
[QUOTE]
using std::cout;
using std::vector;[/QUOTE]
[QUOTE]
enum {DATASIZE = 20};
typedef unsigned char data_t[DATASIZE];[/QUOTE]
[QUOTE]
int setvalue(UInt8 *pValue)
{
for (int i = 0; i < DATASIZE; i++)
pValue[i] = '1' + i;
return 0;
}[/QUOTE]
[QUOTE]
void displayvalue(UInt8 *pValue)
{
for (int i = 0; i < DATASIZE; i++)
cout << pValue[i] << "\n";
}[/QUOTE]
[QUOTE]
int main(int argc, char* argv[])
{
enum {MAX_PROC = 5, MAX_OBJECT = 10000};[/QUOTE]
[QUOTE]
// I want to use vector instead of array because the array size is
determined by a variable
//data_t data[MAX_PROC][MAX_OBJECT];[/QUOTE]
[QUOTE]
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;
} [QUOTE]

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[/QUOTE]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top