By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,835 Members | 1,488 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,835 IT Pros & Developers. It's quick & easy.

compile error: new : cannot specify initializer for arrays

P: n/a
Expand|Select|Wrap|Line Numbers
  1. #include <vector>
  2. #include <iostream>
  3.  
  4. using std::cout;
  5. using std::vector;
  6.  
  7. enum {DATASIZE = 20};
  8. typedef unsigned char data_t[DATASIZE];
  9.  
  10.  
  11. int setvalue(UInt8 *pValue)
  12. {
  13. for (int i = 0; i < DATASIZE; i++)
  14. pValue[i] = '1' + i;
  15. return 0;
  16. }
  17.  
  18. void displayvalue(UInt8 *pValue)
  19. {
  20. for (int i = 0; i < DATASIZE; i++)
  21. cout << pValue[i] << "\n";
  22. }
  23.  
  24.  
  25. int main(int argc, char* argv[])
  26. {
  27. enum {MAX_PROC = 5, MAX_OBJECT = 10000};
  28.  
  29. // I want to use vector instead of array because the array size is
  30. determined by a variable
  31. //data_t data[MAX_PROC][MAX_OBJECT];
  32.  
  33. vector<data_tv[MAX_PROC] (10); // compile error here!
  34. for (int i = 0; i < MAX_PROC; i++) {
  35. setvalue(v[i][0]);
  36. }
  37. cout << "data\n";
  38. for (i = 0; i < MAX_PROC; i++) {
  39. displayvalue(v[i][0]);
  40. }
  41.  
  42. return 0;
  43. }
  44.  
  45.  
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.

Feb 1 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a

wo********@yahoo.ca napsal:
Expand|Select|Wrap|Line Numbers
  1. #include <vector>
  2. #include <iostream>
  3. using std::cout;
  4. using std::vector;
  5. enum {DATASIZE = 20};
  6. typedef unsigned char data_t[DATASIZE];
  7. int setvalue(UInt8 *pValue)
  8. {
  9.     for (int i = 0; i < DATASIZE; i++)
  10.        pValue[i] = '1' + i;
  11.     return 0;
  12. }
  13. void displayvalue(UInt8 *pValue)
  14. {
  15.     for (int i = 0; i < DATASIZE; i++)
  16.         cout << pValue[i] << "\n";
  17. }
  18. int main(int argc, char* argv[])
  19. {
  20.     enum {MAX_PROC = 5, MAX_OBJECT = 10000};
  21.     // I want to use vector instead of array because the array size is
  22. determined by a variable
  23.     //data_t data[MAX_PROC][MAX_OBJECT];
  24.     vector<data_tv[MAX_PROC] (10); // compile error here!
  25.     for (int i = 0; i < MAX_PROC; i++) {
  26.        setvalue(v[i][0]);
  27.     }
  28.     cout << "data\n";
  29.     for (i = 0; i < MAX_PROC; i++) {
  30.         displayvalue(v[i][0]);
  31.     }
  32.     return 0;
  33. }
  34.  

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.
I think you have to define some wrapper class, because you cannot
simply assign arrays. I made some modifications to make your example
compilable, although I do not know wheter the result is that, what
you've expected.

#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_tv[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]);
}
}

Feb 1 '07 #2

P: n/a
On Feb 1, 4:02 pm, "Ondra Holub" <ondra.ho...@post.czwrote:
wong_po...@yahoo.ca napsal:
Expand|Select|Wrap|Line Numbers
  1.  #include <vector>
  2.  #include <iostream>
Expand|Select|Wrap|Line Numbers
  1.         
  2.                  using std::cout;
  3.  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_tv[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.

    I think you have to define some wrapper class, because you cannot
    simply assign arrays. I made some modifications to make your example
    compilable, although I do not know wheter the result is that, what
    you've expected.

    #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_tv[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]);
    }

    } Alternative:
    vector<data_tv[MAX_PROC]; // this will compile
    for (int i = 0; i < MAX_PROC; i++) {
    setvalue(v[i][0]); // run time error
    }

    If I do not declare their size at compile time, then there is a core
    dump at setvalue(v[i][0]);
    I cannot change the function setvalue.
    What can I do?

    Feb 1 '07 #3

    P: n/a
    wo********@yahoo.ca wrote:
    vector<data_tv[MAX_PROC]; // this will compile
    for (int i = 0; i < MAX_PROC; i++) {
    setvalue(v[i][0]); // run time error
    Each of the vectors has size 0, so there is no [0] element in v[i].
    }

    If I do not declare their size at compile time, then there is a core
    dump at setvalue(v[i][0]);
    I cannot change the function setvalue.
    What can I do?
    You can have a vector of vectors:

    vector<vector<data_t v(MAX_PROC, vector<data_t>(1));

    then each of 'v' vectors will have a single default-initialised
    element of type 'data_t'.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Feb 1 '07 #4

    P: n/a
    On Feb 1, 6:18 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
    wong_po...@yahoo.ca wrote:
    vector<data_tv[MAX_PROC]; // this will compile
    for (int i = 0; i < MAX_PROC; i++) {
    setvalue(v[i][0]); // run time error

    Each of the vectors has size 0, so there is no [0] element in v[i].
    }
    If I do not declare their size at compile time, then there is a core
    dump at setvalue(v[i][0]);
    I cannot change the function setvalue.
    What can I do?

    You can have a vector of vectors:

    vector<vector<data_t v(MAX_PROC, vector<data_t>(1));

    then each of 'v' vectors will have a single default-initialised
    element of type 'data_t'.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    I tried two definitions:
    1.
    vector<vector<data_t v(MAX_PROC, vector<data_t>(1));
    will get these compile errors:
    c:\program files\microsoft visual studio\vc98\include\vector(41) :
    error C2440: 'type cast' : cannot convert from 'int' to 'unsigned char
    [20]'
    There are no conversions to array types, although there are
    conversions to references or pointers to arrays
    C:\C++\WINDOWS\vector\vectorpost.cpp(41) : error C2040: 'v' : 'class
    std::vector<class std::vector<unsigned char [20],class
    std::allocator<unsigned char [20],class std::allocator<class
    std::vector<unsigned char [20],class std::allocator<unsigned
    char [20] ' differs in levels of indirection from 'class
    std::vector<unsigned char [20],class std::allocator<unsigned char
    [20] [5]'

    2.
    data_t default_data = {'0', '0'};
    vector<vector<data_t v(MAX_PROC, vector<data_t>(default_data));
    will get these compile errors:
    C:\C++\WINDOWS\vector\vectorpost.cpp(42) : error C2440: 'type cast' :
    cannot convert from 'unsigned char [20]' to 'class
    std::vector<unsigned char [20],class std::allocator<unsigned char
    [20]'
    No constructor could take the source type, or constructor
    overload resolution was ambiguous
    C:\C++\WINDOWS\vector\vectorpost.cpp(42) : error C2040: 'v' : 'class
    std::vector<class std::vector<unsigned char [20],class
    std::allocator<unsigned char [20],class std::allocator<class
    std::vector<unsigned char [20],class std::allocator<unsigned
    char [20] ' differs in levels of indirection from 'class
    std::vector<unsigned char [20],class std::allocator<unsigned char
    [20] [5]'

    Feb 2 '07 #5

    P: n/a
    wo********@yahoo.ca wrote:
    On Feb 1, 6:18 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
    >wong_po...@yahoo.ca wrote:
    >>vector<data_tv[MAX_PROC]; // this will compile
    for (int i = 0; i < MAX_PROC; i++) {
    setvalue(v[i][0]); // run time error

    Each of the vectors has size 0, so there is no [0] element in v[i].
    >> }
    >>If I do not declare their size at compile time, then there is a core
    dump at setvalue(v[i][0]);
    I cannot change the function setvalue.
    What can I do?

    You can have a vector of vectors:

    vector<vector<data_t v(MAX_PROC, vector<data_t>(1));

    then each of 'v' vectors will have a single default-initialised
    element of type 'data_t'.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask

    I tried two definitions:
    1.
    vector<vector<data_t v(MAX_PROC, vector<data_t>(1));
    will get these compile errors:
    c:\program files\microsoft visual studio\vc98\include\vector(41) :
    error C2440: 'type cast' : cannot convert from 'int' to 'unsigned char
    [20]'
    [..]

    Since I have no idea what your 'data_t' looks like, I don't know whether
    it can be default-initialised. Still, try

    vector<vector<data_t v(MAX_PROC, vector<data_t>(1, data_t()));

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Feb 2 '07 #6

    This discussion thread is closed

    Replies have been disabled for this discussion.