473,385 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

compile error: new : cannot specify initializer for arrays

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
5 5008

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
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
    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
    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
    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 thread has been closed and replies have been disabled. Please start a new discussion.

    Similar topics

    5
    by: xuatla | last post by:
    Hi, I encountered the following compile error of c++ and hope to get your help. test2.cpp: In member function `CTest CTest::operator+=(CTest&)': test2.cpp:79: error: no match for 'operator='...
    1
    by: Tony Johansson | last post by:
    Hello! I get compile error when compiling using the command javac from the command terminal window(CMD). I have just two classes which are called HelloWorld.java and Slask.java. I have both...
    2
    by: Tony Johansson | last post by:
    Hello! I get compile error when compiling using the command javac from the command terminal window(CMD). I have just two classes which are called HelloWorld.java and Slask.java. I have both...
    3
    by: Peter | last post by:
    Hi, I am trying to compile an existing project (originally c) in .NET (rename .c files to .cpp). After fixing some problems, here are the ones that I don't know how to deal with:...
    2
    by: wong_powah | last post by:
    #include <vector> #include <iostream> using std::cout; using std::vector; enum {DATASIZE = 20}; typedef unsigned char data_t;
    1
    by: George2 | last post by:
    Hello everyone, Why the below code segment will result in compile error? When I change code to the comment one (constructor), it can compile. The compiler is too stupid? :-) I am using...
    2
    by: akhilesh.noida | last post by:
    I am trying to compile glibc-2.5 for ARM based board. But I am getting errors while configuring it. Please check and give your inputs for resolving this. configure command : $...
    0
    by: SnehaAgrawal | last post by:
    Hi see if u could help me with the following I am accessing a table which is on another DB. But when I write Select LinkNo from ServerName.DBName.dbo.LinkMaster with(XLOck) where Linkid=25 It...
    7
    by: Arjuna | last post by:
    I have a class template (ExclusiveMap) which takes two type parameters C1 and C2 and declares two private members map<C1, C2> and map<C2, C1> (both STL maps). In test code I instantiate this class...
    1
    by: CloudSolutions | last post by:
    Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
    0
    by: Faith0G | last post by:
    I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
    0
    isladogs
    by: isladogs | last post by:
    The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
    0
    by: Charles Arthur | last post by:
    How do i turn on java script on a villaon, callus and itel keypad mobile phone
    0
    by: aa123db | last post by:
    Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
    0
    by: ryjfgjl | last post by:
    In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
    0
    BarryA
    by: BarryA | last post by:
    What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
    1
    by: nemocccc | last post by:
    hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
    1
    by: Sonnysonu | last post by:
    This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

    By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

    To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.