Solved

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

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

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 30

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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.
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 perform CRUD operations on a MySql database.

708 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

15 Experts available now in Live!

Get 1:1 Help Now