473,783 Members | 2,516 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1896

"Carl Ribbegaardh" <ca************ *********@hotma il.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************ *********@hotma il.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******@kanga roologic.com> wrote in message
news:c0******** *****@ID-216073.news.uni-berlin.de...

"Carl Ribbegaardh" <ca************ *********@hotma il.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************ *********@hotma il.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******** ************@tw ister2.libero.i t...
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
3135
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
5021
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 signature. When developing this lib, I figured that the pointer-to-member-function, although seemingly an attractive solution, does not work well for us.
15
3493
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 is a small program I wrote to check if I could get the address of the function from the member-function-pointer, so that I could pass it to the function as a normal function-pointer.
6
3180
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(); TOnMsgReceive *OnMsgReceive; };
3
1953
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
1663
by: Mirko Puhic | last post by:
Is there a way to properly do this? struct Derived; struct Base{ Derived der; };
1
3978
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 arguments, is this VC specific or a standard c++ behavior? the code looks like this: class Base { public: int member; };
2
3929
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 see how I should use pointers to members. Basically, I know how to get a member if I have an object and a pointer-to-member (obj.*ptr instead of obj + offset), but I don't know how to do the opposite: getting the object from a member address and...
5
4663
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
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10315
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10083
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9946
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.