c++ - Can't convert from one data type to the same? -
i'm trying implement own set template, , have issues trying breadth- first search using queue template works independently.
the weird part error in set template when trying compile. why can't convert 1 pointer different 1 same data type?
error c2440: '=' : cannot convert 'set<t>::node<t> *' 'set<t>::node<t> *' [ t=std::string ] types pointed unrelated; conversion requires reinterpret_cast, c-style cast or function-style cast c:\users\programming\set\set.h(96) : while compiling class template member function 'void set<t>::print(set<t>::node<t> *)' [ t=std::string ] c:\users\programming\set\main.cpp(13) : see reference class template instantiation 'set<t>' being compiled [ t=std::string ] queue class template
template <typename t> class queue ... t* front() { if (first != null) return first->item; else return null; } set class template
template <typename t> class set ... queue<node<t> *> q; void print(node<t> *p) { q.push(p); while (p != null) { cout << p->item << "(" << p->height << ") "; if (p->left != null) q.push(p->left); if (p->right != null) q.push(p->right); if (!q.size()) { // error @ line p = q.front(); q.pop(); } else p = null; } cout << endl; }
your queue class instantiated node<t>* type ... attempting return pointer type t queue<t>::front method. if instantating queue<t> class t=node<t>*, need return type t front method, not t*. change front method signature following:
template <typename t> class queue ... t front() { if (first != null) return first->item; else return null; } now cause bunch of problems if t not pointer type ... therefore may want create specialization of queue<t>::front method cases t pointer type. instance:
//pointer-type specialization template<typename t> t queue<t*>::front() { if (first != null) return first->item; else return null; } //non-specialized version t not pointer-type template<typename t> t* queue<t>::front() { if (first != null) return &(first->item); else return null; }
Comments
Post a Comment