Include problems

V

Victor Bazarov

Nevyn said:
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
 
N

Nevyn

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
 
V

Victor Bazarov

Nevyn said:
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
 
N

Nevyn

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
 
V

Victor Bazarov

Nevyn said:
[...]
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;
}
 
A

Agent Mulder

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top