473,804 Members | 2,202 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5048

wo********@yaho o.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(UI nt8 *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...@po st.czwrote:
wong_po...@yaho o.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(UI nt8 *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********@yaho o.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<d ata_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...@com Acast.netwrote:
    wong_po...@yaho o.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<d ata_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<d ata_t v(MAX_PROC, vector<data_t>( 1));
    will get these compile errors:
    c:\program files\microsoft visual studio\vc98\inc lude\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\vectorpo st.cpp(41) : error C2040: 'v' : 'class
    std::vector<cla ss std::vector<uns igned char [20],class
    std::allocator< unsigned char [20],class std::allocator< class
    std::vector<uns igned char [20],class std::allocator< unsigned
    char [20] ' differs in levels of indirection from 'class
    std::vector<uns igned char [20],class std::allocator< unsigned char
    [20] [5]'

    2.
    data_t default_data = {'0', '0'};
    vector<vector<d ata_t v(MAX_PROC, vector<data_t>( default_data));
    will get these compile errors:
    C:\C++\WINDOWS\ vector\vectorpo st.cpp(42) : error C2440: 'type cast' :
    cannot convert from 'unsigned char [20]' to 'class
    std::vector<uns igned 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\vectorpo st.cpp(42) : error C2040: 'v' : 'class
    std::vector<cla ss std::vector<uns igned char [20],class
    std::allocator< unsigned char [20],class std::allocator< class
    std::vector<uns igned char [20],class std::allocator< unsigned
    char [20] ' differs in levels of indirection from 'class
    std::vector<uns igned char [20],class std::allocator< unsigned char
    [20] [5]'

    Feb 2 '07 #5
    wo********@yaho o.ca wrote:
    On Feb 1, 6:18 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
    >wong_po...@yah oo.ca wrote:
    >>vector<data_t v[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<d ata_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<d ata_t v(MAX_PROC, vector<data_t>( 1));
    will get these compile errors:
    c:\program files\microsoft visual studio\vc98\inc lude\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<d ata_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
    2154
    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=' in '*this = CTest::operator+(CTest&)((+t2))' test2.cpp:49: error: candidates are: CTest CTest::operator=(CTest&) make: *** Error 1
    1
    24808
    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 classes in the directory called temp and I do cd temp to this directory. Then I do javac HelloWorld.java Now I get the compile error HelloWorld.java:8 cannot resolve symbol
    2
    6973
    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 classes in the directory called temp and I do cd temp to this directory. Then I do javac HelloWorld.java Now I get the compile error HelloWorld.java:9 cannot find symbol
    3
    3208
    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: -------------------------------------------------------------------------- .... .... \myfile.cpp(987) : error C2440: 'initializing' : cannot convert from 'int (__cdecl *)(mytype *,char *,int)' to 'int (__cdecl *)(void)'
    2
    386
    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
    4509
    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 Visual Studio 2005. --------------------
    2
    6366
    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 : $ ../glibc-2.5/configure --prefix=/mnt/new/Mars/glibc_HQ_test/GLIBC/ install/ --with-__thread --enable-kernel=2.6.11 --enable-shared
    0
    1878
    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 gives me the following error Cannot specify an index or locking hint for a remote data source May be the error is due to I m trying to lock a row which is on another server...Could someone suggest me any solution?
    7
    3516
    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 template providing types int and string for C1 and C2 respectively. The test code compiles cleanly using GCC 3.2 however when I pass it through HPUX aCC I get error messages about the inability to convert strings to ints. I can appreciate that...
    0
    9715
    marktang
    by: marktang | last post by:
    ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
    0
    9595
    by: Hystou | last post by:
    Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
    1
    10354
    by: Hystou | last post by:
    Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
    0
    9175
    agi2029
    by: agi2029 | last post by:
    Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
    0
    6867
    by: conductexam | last post by:
    I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
    0
    5535
    by: TSSRALBI | last post by:
    Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
    0
    5673
    by: adsilva | last post by:
    A Windows Forms form does not have the event Unload, like VB6. What one acts like?
    1
    4313
    by: 6302768590 | last post by:
    Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
    3
    3002
    bsmnconsultancy
    by: bsmnconsultancy | last post by:
    In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

    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.