Include problems

Discussion in 'C++' started by Nevyn, Aug 11, 2003.

  1. Nevyn

    Nevyn Guest

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

    file list.h
    -----------
    class listOfItem : public QPtrList<Item>
    {
     
    Nevyn, Aug 11, 2003
    #1
    1. Advertising

  2. "Nevyn" <> wrote...
    > 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?


    For starters, you should learn to always post real code by copy-
    and-paste operation instead of typing it into the message.

    > whatever i tried resulted in errors


    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
     
    Victor Bazarov, Aug 11, 2003
    #2
    1. Advertising

  3. Nevyn

    Nevyn Guest

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


    > 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.
    >

    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
     
    Nevyn, Aug 11, 2003
    #3
  4. "Nevyn" <> wrote...
    > On Mon, 11 Aug 2003 10:03:22 -0400, Victor Bazarov wrote:
    >
    >
    > > 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.
    > >

    > 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?


    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
     
    Victor Bazarov, Aug 11, 2003
    #4
  5. Nevyn

    Nevyn Guest

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


    > 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.
    >

    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
     
    Nevyn, Aug 11, 2003
    #5
  6. "Nevyn" <> wrote...
    > [...]
    > Now i am wondering (just out of curiosity) is there a way to solve the
    > above problem without using pointers?


    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
     
    Victor Bazarov, Aug 11, 2003
    #6
  7. Nevyn

    Agent Mulder Guest

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

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

    -X
     
    Agent Mulder, Aug 12, 2003
    #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. Danny Anderson
    Replies:
    5
    Views:
    507
    Victor Bazarov
    Aug 15, 2003
  2. Rolf Magnus
    Replies:
    2
    Views:
    605
    Karl Heinz Buchegger
    Nov 28, 2003
  3. Elie Nader
    Replies:
    1
    Views:
    642
  4. Aguilar, James
    Replies:
    2
    Views:
    691
    Aguilar, James
    Jul 16, 2004
  5. Andreas Bogenberger
    Replies:
    3
    Views:
    932
    Andreas Bogenberger
    Feb 22, 2008
Loading...

Share This Page