Member pointer to template class object?

C

Carl Ribbegaardh

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
 
J

John Harrison

Carl Ribbegaardh said:
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
 
J

Jonathan Turkanis

Carl Ribbegaardh said:
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
 
C

Carl Ribbegaardh

Comments inline :)

Jonathan Turkanis said:
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 :)
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 :)
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
 
C

Carl Ribbegaardh

Comments inline :)

John Harrison said:
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


Thanks a lot for your help!

/Carl
 
A

Alberto Barbati

Carl said:
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
 
C

Carl Ribbegaardh

Alberto Barbati said:
The syntax you are using is correct. The problem probably lies
elsewhere. It might help if you could post the actual error messages.


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!
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top