c++ - Conversion in template from Classes inherit a base class to base -
i have node template link list. want create in restaurant project list of item use node linked list reference counter, special inherit beverage , edible. bevrage , edible inherit item. have menu have dishes include 3 class , need change or add dish manager. problem because can't convert class base node<item>*
.
i want use function addnewtitem()
;
#ifndef menue_h #define menue_h #include "node.h" class menue { public: menue(); node<item>* _head; void deleteitem(node<item>* item); node<item>* getitem(int i); void addnewtitem(node<item>& nextnode) {if(_head) _head->addnode(nextnode); else _head=nextnode;} void printmenue(); void deleteitem(int i); ~menue(); protected: private: }; #endif // menue_h
call class
#ifndef meneger_h #define meneger_h #include "node.h" #include "waiter.h" #include "menue.h" class meneger: public waiter { public: meneger(); ~meneger(); void edititem (node<edible>& newitem){ _mymenue->addnewtitem(newitem);} void edititem (node<special>& newitem){ _mymenue->addnewtitem(newitem);} void edititem (node<beverage>& newitem){ _mymenue->addnewtitem(newitem);} protected: private: int _amounttable; int _weiterid; table* _table; menue* _mymenue; }; #endif // meneger_h
this node class link list
i want use function operator node<newtype>()
conversion doesn't work
#ifndef node_h #define node_h #include "item.h" #include "edible.h" #include "beverage.h" #include "special.h" template <class t> class node { public: t* _item; node* _next; int _refcount; node():_next(null),_refcount(0),_item(null){} node(t* item=0,node<t>* next=0):_next(next),_item(item),_refcount(1){} ~node(){ if(_item)delete _item;_item=null;} t& operator* (){return *_item;} t* operator-> (){return _item;} void addrefcount() {_refcount++;} void addnode(node<t>* newitem); int removeitem(){return --_refcount;} template<class newtype> // template function operator node<newtype>() // implicit conversion ops. { return node<newtype>(_item); } private: }; template <class t> inline void node<t>::addnode(node<t>* newitem) { if(newitem==null) return; newitem->_next=_next; _next=newitem; } #endif // node_h
i don't know if it's problem you're looking but... it's problem.
look @ method menue::addnewitem()
void addnewtitem(node<item>& nextnode) {if(_head) _head->addnode(nextnode); else _head=nextnode;}
i don't know item
_head
pointer node<item>
. , node::addnode()
receive node<t>
(node<item>
, in case) pointer. nextnode
reference node<item>
, not pointer node<item>
so, when write
_head->addnode(nextnode);
you're trying pass node<item>
method expect pointer node<item>
; , when write
_head = nextnode;
you're trying assign node<item>
variable of type pointer node<item>
.
your intention was
_head->addnode(&nextnode); _head = & nextnode;
?
Comments
Post a Comment