Solved

Syntax error while declaring a vector of vectors with an initial size

Posted on 2008-10-01
5
533 Views
Last Modified: 2013-12-14
While trying to declare a vector of vectors with an initial size Visual studio reports that the code below is a syntax error.

error C2059: syntax error : 'constant'

In fact I get the same error when trying to declare a vector of objects with an initial size.

Any help with where I am going wrong would be appreciated.


vector<vector<MyObject> > m_last_received_object(2);
vector<MyObject> m_last_processed_object(2);

Open in new window

0
Comment
Question by:nanderEire
  • 2
  • 2
5 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22612687
you can do it as:
vector< vector<MyObject> > m_last_received_object(10, vector<MyObject>(5,0));    

this will create a 10x5 arrangement
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 22612699
Hi nanderEire,

I guess it has to do something with the declaration/definition of 'MyObject' - i.e. it works with 'int' and I tested with classes even without problems:

> std::vector <int> v( 2 );
> std::vector < std::vector <int> > v1( 2 );

> class MyObj
> {
> };
> ...
> std::vector <MyObj> v( 2 );
> std::vector < std::vector <MyObj> > v1( 2 );

Both of these compile fine ...

ZOPPO
0
 

Author Comment

by:nanderEire
ID: 22612749
Could it be to do with the fact that I am declaring this vector as a private member of a class?

Am I allowed to give it an initial size at that point?

Apologies for not including that information initially.
class MyOtherObject{
 
private:
vector<vector<MyObject> > m_last_received_object(2);
}

Open in new window

0
 
LVL 31

Accepted Solution

by:
Zoppo earned 150 total points
ID: 22612760
You can't instantiate a member in a class declaration ...

You'll have to instantiate it in a constructor of the class, i.e.:

class MyOtherObject{
...
 vector<vector<MyObject>> m_last_recieved_object;
 MyOtherObject() : m_last_recieved_object( 2 )
 {
 }
};

Hope that helps,

ZOPPO
0
 

Author Closing Comment

by:nanderEire
ID: 31501885
Thanks for working through that with me.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
What is the best editor for PowerShell scripts? 7 95
Netbeans Open project initial dir 5 44
Eclipse and java version isssue 2 44
debug as  junit test 4 82
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question