473,324 Members | 2,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Problem instatiating a template container

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 ';'.
May 29 '06 #1
4 2592
Ulrich Hobelmann wrote:
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
'List<sometype*>*' in assignment"
'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 ';'.

May 29 '06 #2
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;

May 29 '06 #3
Ulrich Hobelmann wrote:
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.

May 29 '06 #4
Rolf Magnus wrote:
Why do you want to allocate the container dynamically?


To store it somewhere else ;)

(in another struct in this case)
(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.


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.
May 29 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Alexander Stippler | last post by:
Hi, I have a little problem to design some template classes in a realizable way. I have some Container classes and some Proxy classes (proxies for elements). Looks like this: // P is the...
4
by: Antonio Carpentieri | last post by:
Hi all, in my previous post I've wrong typed some tems.. this is the corrected post. in a aspx page I have a repeater like this: <asp:repeater id=repeaterResults runat="server"...
3
by: shapper | last post by:
Hello, In my GridView I have a HyperLink Field where I set the DataNavigateUrlFormaString MyHyperLinkField.DataNavigateUrlFormatString = "~\RSS.ashx?Channel={0}&Culture=" &...
7
by: aaragon | last post by:
Hi everyone, The idea is quite simple: generate a container with random values in it. For that, I decided to create a class that I called RandomContainer that inherits from a container (with...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.