Solved

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

Posted on 2008-10-01
5
537 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Object Data Source 4 143
Visual Studio - Accept Button gets border 3 77
Change local server setting in php 6 118
Export all vba modules from outlook vbaproject.otm 7 72
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ā€¦
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

752 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