我試圖實現我自己的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;
}
_cannot從轉換 '設置 ::節點 *' 到 '設置 ::節點 *' _ **與'T = ...'和'T = ...' _ ** –
我懷疑這不是整個錯誤信息。 'q'的類型是什麼,'front'返回的是什麼? – molbdnilo
也許'q'是一組指針指向const? –