Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-10-01
5
Medium Priority
?
540 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 450 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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 column That will then direct you to their download page. From that p…
The viewer will learn how to synchronize PHP projects with a remote server 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.

705 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