Solved

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

Posted on 2008-10-01
5
532 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
notepad++ shortcuts 6 135
AJAX function() is succeeding, but not posting correctly 6 70
Netbeans Open project initial dir 5 42
CKEditor - will not function 7 61
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
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…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now