Member pointer to template class object?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Carl Ribbegaardh

    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



  • John Harrison

    #2
    Re: Member pointer to template class object?


    "Carl Ribbegaardh" <carl_ribbegaar dh.nospam@hotma il.com> wrote in message
    news:c0tt43$1a4 2j2$1@ID-111741.news.uni-berlin.de...[color=blue]
    > I'm sorry if I ask something that's in the FAQ but I've searched google[/color]
    for[color=blue]
    > 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[/color]
    is[color=blue]
    > 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
    >[/color]

    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




    Comment

    • Jonathan Turkanis

      #3
      Re: Member pointer to template class object?


      "Carl Ribbegaardh" <carl_ribbegaar dh.nospam@hotma il.com> wrote in
      message news:c0tt43$1a4 2j2$1@ID-111741.news.uni-berlin.de...[color=blue]
      > I'm sorry if I ask something that's in the FAQ but I've searched[/color]
      google for[color=blue]
      > hours now. Maybe I'm looking for the wrong terms?
      >
      > I have written a linked list class which is a template class. I[/color]

      Why not use std::list?

      [color=blue]
      > 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[/color]
      bad...[color=blue]
      >
      > class User{
      > private:
      > LinkList<Stuff* >* m_pStuff;
      > };
      > and in the constructor:
      > User::User()
      > {
      > m_pStuff = new LinkList<Stuff* >();
      > }[/color]

      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.
      [color=blue]
      >
      > This gives all sorts of errors which seems to me like the member[/color]
      pointer is[color=blue]
      > syntactically incorrect.[/color]

      Tell us what the errors say.

      Jonathan


      Comment

      • Carl Ribbegaardh

        #4
        Re: Member pointer to template class object?

        Comments inline :)

        "Jonathan Turkanis" <technews@kanga roologic.com> wrote in message
        news:c0tvt5$1bp aqi$1@ID-216073.news.uni-berlin.de...[color=blue]
        >
        > "Carl Ribbegaardh" <carl_ribbegaar dh.nospam@hotma il.com> wrote in
        > message news:c0tt43$1a4 2j2$1@ID-111741.news.uni-berlin.de...[color=green]
        > > I'm sorry if I ask something that's in the FAQ but I've searched[/color]
        > google for[color=green]
        > > hours now. Maybe I'm looking for the wrong terms?
        > >
        > > I have written a linked list class which is a template class. I[/color]
        >
        > Why not use std::list?[/color]

        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 :)
        [color=blue]
        >
        >[color=green]
        > > 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[/color]
        > bad...[color=green]
        > >
        > > class User{
        > > private:
        > > LinkList<Stuff* >* m_pStuff;
        > > };
        > > and in the constructor:
        > > User::User()
        > > {
        > > m_pStuff = new LinkList<Stuff* >();
        > > }[/color]
        >
        > 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.
        >[/color]

        I'm still learning :)
        [color=blue][color=green]
        > >
        > > This gives all sorts of errors which seems to me like the member[/color]
        > pointer is[color=green]
        > > syntactically incorrect.[/color]
        >
        > Tell us what the errors say.[/color]

        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
        [color=blue]
        >
        > Jonathan
        >
        >[/color]


        Comment

        • Carl Ribbegaardh

          #5
          Re: Member pointer to template class object?

          Comments inline :)

          "John Harrison" <john_andronicu s@hotmail.com> wrote in message
          news:c0tvom$1bo cr8$1@ID-196037.news.uni-berlin.de...[color=blue]
          >
          > "Carl Ribbegaardh" <carl_ribbegaar dh.nospam@hotma il.com> wrote in message
          > news:c0tt43$1a4 2j2$1@ID-111741.news.uni-berlin.de...[color=green]
          > > I'm sorry if I ask something that's in the FAQ but I've searched google[/color]
          > for[color=green]
          > > hours now. Maybe I'm looking for the wrong terms?
          > >
          > > I have written a linked list class which is a template class. I've[/color][/color]
          pretty[color=blue][color=green]
          > > 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[/color]
          > is[color=green]
          > > syntactically incorrect. How can I have a member pointer variable[/color][/color]
          pointing[color=blue][color=green]
          > > to a template class object?
          > > What's the correct syntax?
          > > Can it be done as an auto_ptr too?
          > >
          > > TIA
          > > /Carl
          > >[/color]
          >
          > There's nothing wrong with the code you've posted. The error is somewhere
          > else.[/color]

          After rebooting it compiles... (was taking a short walk to clear my head)
          [color=blue]
          >
          > How about posting a complete example, or at least posting the error[/color]
          messages[color=blue]
          > 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).[/color]

          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...
          [color=blue]
          > And of course you can do this as an auto_ptr.
          >[/color]

          Great! :D

          [color=blue]
          > john
          >[/color]

          Thanks a lot for your help!

          /Carl


          Comment

          • Alberto Barbati

            #6
            Re: Member pointer to template class object?

            Carl Ribbegaardh wrote:[color=blue]
            > 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?[/color]

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

            Of course, yes.

            Alberto

            Comment

            • Carl Ribbegaardh

              #7
              Re: Member pointer to template class object?

              "Alberto Barbati" <AlbertoBarbati @libero.it> wrote in message
              news:skwYb.1460 0$Kc3.439241@tw ister2.libero.i t...[color=blue]
              > Carl Ribbegaardh wrote:[color=green]
              > > This gives all sorts of errors which seems to me like the member pointer[/color][/color]
              is[color=blue][color=green]
              > > syntactically incorrect. How can I have a member pointer variable[/color][/color]
              pointing[color=blue][color=green]
              > > to a template class object?
              > > What's the correct syntax?[/color]
              >
              > The syntax you are using is correct. The problem probably lies
              > elsewhere. It might help if you could post the actual error messages.
              >[color=green]
              > > Can it be done as an auto_ptr too?[/color]
              >
              > Of course, yes.
              >
              > Alberto[/color]

              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!


              Comment

              Working...