Include problems

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

    Include problems

    Hi, i've got a problem with the following declarations

    file list.h
    -----------
    class listOfItem : public QPtrList<Item>
    {
  • Victor Bazarov

    #2
    Re: Include problems

    "Nevyn" <nevynZEROSPAM@ hotmail.com> wrote...[color=blue]
    > Hi, i've got a problem with the following declarations
    >
    > file list.h
    > -----------
    > class listOfItem : public QPtrList<Item>
    > {
    > .
    > .
    > .
    > }
    >
    > ----
    >
    > file multiple.h
    > --------------------
    > class Item
    > {
    > private:
    > listOfItem: list1, list2;
    > .
    > .
    > .
    > }
    >
    > how should i include the above files?[/color]

    For starters, you should learn to always post real code by copy-
    and-paste operation instead of typing it into the message.
    [color=blue]
    > whatever i tried resulted in errors[/color]

    Perhaps you should change the 'Item' to contain a pointers to lists:

    class listOfItem;

    class Item
    {
    listOfItem *list1, *list2;
    ...
    };

    That allows you to only forward-declare the class instead of having
    to include the definition.

    Victor


    Comment

    • Nevyn

      #3
      Re: Include problems

      On Mon, 11 Aug 2003 10:03:22 -0400, Victor Bazarov wrote:

      [color=blue]
      > Perhaps you should change the 'Item' to contain a pointers to lists:
      >
      > class listOfItem;
      >
      > class Item
      > {
      > listOfItem *list1, *list2;
      > ...
      > };
      >
      > That allows you to only forward-declare the class instead of having to
      > include the definition.
      >[/color]
      Thanks for your answer Victor but given that i must use list1 & list2 in
      some method of Item (e.g. count total elements in lists) i have to call
      the 'new' statement for those listOfItem in the Item constructor... thus i
      always end with the same problems... or am i missing something?

      thanks a lot

      Comment

      • Victor Bazarov

        #4
        Re: Include problems

        "Nevyn" <nevynZEROSPAM@ hotmail.com> wrote...[color=blue]
        > On Mon, 11 Aug 2003 10:03:22 -0400, Victor Bazarov wrote:
        >
        >[color=green]
        > > Perhaps you should change the 'Item' to contain a pointers to lists:
        > >
        > > class listOfItem;
        > >
        > > class Item
        > > {
        > > listOfItem *list1, *list2;
        > > ...
        > > };
        > >
        > > That allows you to only forward-declare the class instead of having to
        > > include the definition.
        > >[/color]
        > Thanks for your answer Victor but given that i must use list1 & list2 in
        > some method of Item (e.g. count total elements in lists) i have to call
        > the 'new' statement for those listOfItem in the Item constructor... thus i
        > always end with the same problems... or am i missing something?[/color]

        Yes, you're missing that you're allowed to put the Item's constructor
        into a separate file and not the 'Item' class definition. The order of
        compilation for Item's constructor would be

        Forward-declare class listOfItem.
        Define class Item.
        Define class listOfItem.
        Define Item::Item.

        Victor


        Comment

        • Nevyn

          #5
          Re: Include problems

          On Mon, 11 Aug 2003 11:50:52 -0400, Victor Bazarov wrote:

          [color=blue]
          > Yes, you're missing that you're allowed to put the Item's constructor
          > into a separate file and not the 'Item' class definition. The order of
          > compilation for Item's constructor would be
          >
          > Forward-declare class listOfItem.
          > Define class Item.
          > Define class listOfItem.
          > Define Item::Item.
          >[/color]
          Nope, that was not what i was missing :-) i was trying instead to define
          class ListOfItem (capital L)

          after having pre-declared
          class listOfItem (no capital) !!!

          silly of me :-(

          Now i am wondering (just out of curiosity) is there a way to solve the
          above problem without using pointers?

          thanks a lot

          Comment

          • Victor Bazarov

            #6
            Re: Include problems

            "Nevyn" <nevynZEROSPAM@ hotmail.com> wrote...[color=blue]
            > [...]
            > Now i am wondering (just out of curiosity) is there a way to solve the
            > above problem without using pointers?[/color]

            Try using references... :-)

            Actually this compiles:
            ---------------------------------------------------- listOfT.h
            template<class T> class listOfT {
            T* head;
            public:
            listOfT() : head(0) {}
            };
            ---------------------------------------------------- Item.h
            #include <listOfT.h>
            class Item;
            typedef listOfT<Item> listOfItem;

            class Item {
            listOfItem list;
            };
            ---------------------------------------------------- main.cpp
            #include <Item.h>
            int main() {
            Item it;
            }
            ---------------------------------------------------------------

            HTH

            Victor



            Comment

            • Agent Mulder

              #7
              Re: Include problems

              Neyvn> how should i include the above files?
              Neyvn> whatever i tried resulted in errors

              #include <vector>
              class Item;
              class listOfItem:publ ic std::vector<Ite m>{};
              class Item {private:listOf Item list1,list2;};
              int main(int argc,char**argv )
              {
              listOfItem listofitem;
              Item item;
              return 0;
              }

              -X


              Comment

              Working...