Member pointer to template class object?

Discussion in 'C++' started by Carl Ribbegaardh, Feb 17, 2004.

  1. 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
    Carl Ribbegaardh, Feb 17, 2004
    #1
    1. Advertising

  2. "Carl Ribbegaardh" <> wrote in message
    news:c0tt43$1a42j2$-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
    John Harrison, Feb 17, 2004
    #2
    1. Advertising

  3. "Carl Ribbegaardh" <> wrote in
    message news:c0tt43$1a42j2$-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
    Jonathan Turkanis, Feb 17, 2004
    #3
  4. Comments inline :)

    "Jonathan Turkanis" <> wrote in message
    news:c0tvt5$1bpaqi$-berlin.de...
    >
    > "Carl Ribbegaardh" <> wrote in
    > message news:c0tt43$1a42j2$-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
    >
    >
    Carl Ribbegaardh, Feb 17, 2004
    #4
  5. Comments inline :)

    "John Harrison" <> wrote in message
    news:c0tvom$1bocr8$-berlin.de...
    >
    > "Carl Ribbegaardh" <> wrote in message
    > news:c0tt43$1a42j2$-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
    Carl Ribbegaardh, Feb 17, 2004
    #5
  6. 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
    Alberto Barbati, Feb 17, 2004
    #6
  7. "Alberto Barbati" <> wrote in message
    news:skwYb.14600$...
    > 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!
    Carl Ribbegaardh, Feb 17, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fraser Ross
    Replies:
    4
    Views:
    1,023
    Fraser Ross
    Aug 14, 2004
  2. christopher diggins
    Replies:
    16
    Views:
    730
    Pete Becker
    May 4, 2005
  3. Stephen Howe
    Replies:
    2
    Views:
    277
    Stephen Howe
    Nov 6, 2012
  4. somenath
    Replies:
    10
    Views:
    258
    James Kanze
    Jul 2, 2013
  5. somenath
    Replies:
    2
    Views:
    143
    somenath
    Aug 29, 2013
Loading...

Share This Page