By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,851 Members | 2,097 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,851 IT Pros & Developers. It's quick & easy.

Include problems

P: n/a
Hi, i've got a problem with the following declarations

file list.h
-----------
class listOfItem : public QPtrList<Item>
{
Jul 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Nevyn" <ne***********@hotmail.com> 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
Jul 19 '05 #2

P: n/a
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
Jul 19 '05 #3

P: n/a
"Nevyn" <ne***********@hotmail.com> 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
Jul 19 '05 #4

P: n/a
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
Jul 19 '05 #5

P: n/a
"Nevyn" <ne***********@hotmail.com> 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

Jul 19 '05 #6

P: n/a
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
Jul 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.