Problem instatiating a template container

U

Ulrich Hobelmann

Hi, how do I correctly instantiate a template container? Everywhere I
look they only mention in-place containers, i.e. construction on the
stack like "List<Bla> myList;".

I tried
List<sometype *> * ls = new List(); which complains (with G++ 4):
expected type specifier before List; cannot convert 'int*' to
'sometype*' in assignment; expected ',' or ';' before 'List'.

(btw: List is my own class, in case you're wondering, but I think a
general answer would be just as fine).

I also tried Java syntax:
List<sometype *> * ls = new List<sometype *>();

but it doesn't work either.

I seriously can't comprehend the error message. Yes, the statement
before the initialization ends with a ';'.
 
U

Ulrich Hobelmann

Ulrich said:
Hi, how do I correctly instantiate a template container? Everywhere I
look they only mention in-place containers, i.e. construction on the
stack like "List<Bla> myList;".

I tried
List<sometype *> * ls = new List(); which complains (with G++ 4):
expected type specifier before List; cannot convert 'int*' to

Sorry, this line should read "cannot convert 'int*' to
 
S

stain

You don't only have to specify the type for the pointer but also for
the call to "new". Hence
List<sometype *> * ls = new List<sometype*>();
should work. Often you do a typedef before instanciting the list. Like
this:
typedef list<int> intlist;
intlist * myList = new intlist();

Also note that the elements contained in the std::list will be stored
on the heap. Only the container object will be on the stack for calls
like:
list<bla> myList;
 
R

Rolf Magnus

Ulrich said:
Hi, how do I correctly instantiate a template container? Everywhere I
look they only mention in-place containers, i.e. construction on the
stack like "List<Bla> myList;".

I tried
List<sometype *> * ls = new List(); which complains (with G++ 4):
expected type specifier before List; cannot convert 'int*' to
'sometype*' in assignment; expected ',' or ';' before 'List'.

Why do you want to allocate the container dynamically?
(btw: List is my own class, in case you're wondering, but I think a
general answer would be just as fine).

I also tried Java syntax:
List<sometype *> * ls = new List<sometype *>();

but it doesn't work either.

What does "doesn't work either" mean? This one looks right.
 
U

Ulrich Hobelmann

Rolf said:
Why do you want to allocate the container dynamically?

To store it somewhere else ;)

(in another struct in this case)
What does "doesn't work either" mean? This one looks right.

Hm, after reading stain's post, I noticed that, too. Maybe the first
time I accidentally didn't save my file, because the error message still
appeared.

Well, thanks for clearing that up... works great now.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top