我正在尝试实现自己的 Set 模板,但在尝试使用独立工作的 Queue 模板进行广度优先搜索时遇到问题。
奇怪的是,我在尝试编译时在我的 Set 模板中遇到了这个错误。为什么它不能从一个指针转换为另一个相同数据类型的指针?
error C2440: '=' : cannot convert from 'Set<T>::Node<T> *' to 'Set<T>::Node<T> *'
with
[
T=std::string
]
Types pointed to are 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> *)'
with
[
T=std::string
]
c:\users\programming\Set\main.cpp(13) : see reference to class template instantiation 'Set<T>' being compiled
with
[
T=std::string
]
队列类模板
template <typename T>
class Queue
...
T* front()
{
if (first != NULL)
return first->item;
else
return NULL;
}
设置类模板
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 is at this line
p = q.front();
q.pop();
}
else
p = NULL;
}
cout << endl;
}