473,378 Members | 1,344 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,378 software developers and data experts.

Member pointer to template class object?

I'm sorry if I ask something that's in the FAQ but I've searched google for
hours now. Maybe I'm looking for the wrong terms?

I have written a linked list class which is a template class. I've pretty
much followed the GOF recipe from the Iterator pattern example.
The follwing works fine in eg main:

LinkList<User*> users;
users.Add(new User("John"));

I can also instantiate objects like this from main:
LinkList<User*>* aUser= new LinkList<User*>();

If I try to have a linked list as a member in the User things go bad...

class User{
private:
LinkList<Stuff*>* m_pStuff;
};
and in the constructor:
User::User()
{
m_pStuff = new LinkList<Stuff*>();
}

This gives all sorts of errors which seems to me like the member pointer is
syntactically incorrect. How can I have a member pointer variable pointing
to a template class object?
What's the correct syntax?
Can it be done as an auto_ptr too?

TIA
/Carl

Jul 22 '05 #1
6 1879

"Carl Ribbegaardh" <ca*********************@hotmail.com> wrote in message
news:c0*************@ID-111741.news.uni-berlin.de...
I'm sorry if I ask something that's in the FAQ but I've searched google for hours now. Maybe I'm looking for the wrong terms?

I have written a linked list class which is a template class. I've pretty
much followed the GOF recipe from the Iterator pattern example.
The follwing works fine in eg main:

LinkList<User*> users;
users.Add(new User("John"));

I can also instantiate objects like this from main:
LinkList<User*>* aUser= new LinkList<User*>();

If I try to have a linked list as a member in the User things go bad...

class User{
private:
LinkList<Stuff*>* m_pStuff;
};
and in the constructor:
User::User()
{
m_pStuff = new LinkList<Stuff*>();
}

This gives all sorts of errors which seems to me like the member pointer is syntactically incorrect. How can I have a member pointer variable pointing
to a template class object?
What's the correct syntax?
Can it be done as an auto_ptr too?

TIA
/Carl


There's nothing wrong with the code you've posted. The error is somewhere
else.

How about posting a complete example, or at least posting the error messages
you get and pointing out which lines of code they apply to. At the moment
you are assuming that the regulars in this group have psychic powers (not
that I wouldn't put that past some of them).

And of course you can do this as an auto_ptr.

john


Jul 22 '05 #2

"Carl Ribbegaardh" <ca*********************@hotmail.com> wrote in
message news:c0*************@ID-111741.news.uni-berlin.de...
I'm sorry if I ask something that's in the FAQ but I've searched google for hours now. Maybe I'm looking for the wrong terms?

I have written a linked list class which is a template class. I
Why not use std::list?

The follwing works fine in eg main:

LinkList<User*> users;
users.Add(new User("John"));

I can also instantiate objects like this from main:
LinkList<User*>* aUser= new LinkList<User*>();

If I try to have a linked list as a member in the User things go bad...
class User{
private:
LinkList<Stuff*>* m_pStuff;
};
and in the constructor:
User::User()
{
m_pStuff = new LinkList<Stuff*>();
}
This looks okay, but I'd need to see the code for LinkList. You should
ask yourself whether you really need pointers, or whether you could
use

LinkList<Stuff> m_stuff;

Possibly you need the member to be a pointer, but not the value_type
of the list, or vice versa. If you can get by without pointers,
everything is cleaner.

This gives all sorts of errors which seems to me like the member pointer is syntactically incorrect.


Tell us what the errors say.

Jonathan
Jul 22 '05 #3
Comments inline :)

"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message
news:c0*************@ID-216073.news.uni-berlin.de...

"Carl Ribbegaardh" <ca*********************@hotmail.com> wrote in
message news:c0*************@ID-111741.news.uni-berlin.de...
I'm sorry if I ask something that's in the FAQ but I've searched google for
hours now. Maybe I'm looking for the wrong terms?

I have written a linked list class which is a template class. I


Why not use std::list?


It's a school laboration. Otherwise I'd definitely use a std::list.
In fact using templates is overkill for the assignment too, but I want it to
be interesting :)

The follwing works fine in eg main:

LinkList<User*> users;
users.Add(new User("John"));

I can also instantiate objects like this from main:
LinkList<User*>* aUser= new LinkList<User*>();

If I try to have a linked list as a member in the User things go bad...

class User{
private:
LinkList<Stuff*>* m_pStuff;
};
and in the constructor:
User::User()
{
m_pStuff = new LinkList<Stuff*>();
}


This looks okay, but I'd need to see the code for LinkList. You should
ask yourself whether you really need pointers, or whether you could
use

LinkList<Stuff> m_stuff;

Possibly you need the member to be a pointer, but not the value_type
of the list, or vice versa. If you can get by without pointers,
everything is cleaner.


I'm still learning :)

This gives all sorts of errors which seems to me like the member

pointer is
syntactically incorrect.


Tell us what the errors say.


Well... After rebooting (I went out for a short walk to clear my head) it
compiles...
*Sigh*
There are no error messages anymore.
It even compiles the way you suggested with an object variable.

The error messages that was before told me that there should be a ; before
the <
Thanks for taking your time!!
/Carl

Jonathan

Jul 22 '05 #4
Comments inline :)

"John Harrison" <jo*************@hotmail.com> wrote in message
news:c0*************@ID-196037.news.uni-berlin.de...

"Carl Ribbegaardh" <ca*********************@hotmail.com> wrote in message
news:c0*************@ID-111741.news.uni-berlin.de...
I'm sorry if I ask something that's in the FAQ but I've searched google for
hours now. Maybe I'm looking for the wrong terms?

I have written a linked list class which is a template class. I've pretty much followed the GOF recipe from the Iterator pattern example.
The follwing works fine in eg main:

LinkList<User*> users;
users.Add(new User("John"));

I can also instantiate objects like this from main:
LinkList<User*>* aUser= new LinkList<User*>();

If I try to have a linked list as a member in the User things go bad...

class User{
private:
LinkList<Stuff*>* m_pStuff;
};
and in the constructor:
User::User()
{
m_pStuff = new LinkList<Stuff*>();
}

This gives all sorts of errors which seems to me like the member pointer

is
syntactically incorrect. How can I have a member pointer variable pointing to a template class object?
What's the correct syntax?
Can it be done as an auto_ptr too?

TIA
/Carl


There's nothing wrong with the code you've posted. The error is somewhere
else.


After rebooting it compiles... (was taking a short walk to clear my head)

How about posting a complete example, or at least posting the error messages you get and pointing out which lines of code they apply to. At the moment
you are assuming that the regulars in this group have psychic powers (not
that I wouldn't put that past some of them).
I thought I was writing syntactically incorrect or doing something that's
wrong in C++, or maybe leaving out something related to the template syntax.
Seems it was a compiler hiccup.
:)

I was really getting grey hair...
And of course you can do this as an auto_ptr.

Great! :D

john


Thanks a lot for your help!

/Carl
Jul 22 '05 #5
Carl Ribbegaardh wrote:
This gives all sorts of errors which seems to me like the member pointer is
syntactically incorrect. How can I have a member pointer variable pointing
to a template class object?
What's the correct syntax?
The syntax you are using is correct. The problem probably lies
elsewhere. It might help if you could post the actual error messages.
Can it be done as an auto_ptr too?


Of course, yes.

Alberto
Jul 22 '05 #6
"Alberto Barbati" <Al************@libero.it> wrote in message
news:sk********************@twister2.libero.it...
Carl Ribbegaardh wrote:
This gives all sorts of errors which seems to me like the member pointer is syntactically incorrect. How can I have a member pointer variable pointing to a template class object?
What's the correct syntax?


The syntax you are using is correct. The problem probably lies
elsewhere. It might help if you could post the actual error messages.
Can it be done as an auto_ptr too?


Of course, yes.

Alberto


After restarting the computer, it compiled...
I'm very happy that I got the syntax correct. I thought I missed some
template declaration or something. :)

Thanks Alberto!
Jul 22 '05 #7

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

Similar topics

4
by: Graham Dumpleton | last post by:
When you have a template class with a static member variable, ie., template<typename T> class handle { public: static void* id() { return &id_; } private: static int id_; };
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
15
by: Albert | last post by:
Hi, I need to pass a pointer-to-member-function as a parameter to a function which takes pointer-to-function as an argument. Is there any way to do it besides overloading the function? Here...
6
by: | last post by:
Say we have the following code defining TMyMsgHandler and TMyClass typedef void (*TOnMsgReceive) (TMyMessage Msg); class TMyMsgHandler { public: TMyMsgHandler(); virtual ~TMyMsgHandler();...
3
by: xuatla | last post by:
Hi, I have a problem about using a class member function as a parameter in another function. What I tried to implement is something like described below: class A { public:
9
by: Mirko Puhic | last post by:
Is there a way to properly do this? struct Derived; struct Base{ Derived der; };
1
by: autumn | last post by:
Hi everybody, I'm having problem passing pointer to member object as template argument, seems VC 2005 does not allow 'pointer to base member' to 'pointer to derived member' conversion in template...
2
by: Imre | last post by:
Hi I know that offsetof is basically a C leftover, and only works for POD types, not classes, so it is recommended that I use pointers to members instead. However, I have a problem where I don't...
5
by: Tim Frink | last post by:
Hi, I'm experimenting with function pointers and found two questions. Let's assume this code: 1 #include <iostream> 2 class A; 3 4 //////////////////////////////////////////// 5 class B
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...
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.