我正在做家庭作业,我们不允许使用任何STL容器.我对LinkedList的实现是一系列与指针链接在一起的节点.我有另一个名为ContinuousList的类,它有一个数据成员LinkedList,其节点包含指向各种其他LinkedLists中的节点的指针.我正在尝试将一个返回指向Node的指针的函数的返回值赋给一个变量,该变量也是指向Node的指针,但它说这是无效的,我不明白为什么我不能做那.
template <typename ValueType>
struct Node
{
Node();
std::string m_key;
ValueType m_value;
Node<ValueType>* m_next;
};
Run Code Online (Sandbox Code Playgroud)
链表类:
template <typename ValueType>
class LinkedList
{
public:
Node<ValueType>* begin()
{
return m_head;
}
private:
Node<ValueType>* m_head;
};
Run Code Online (Sandbox Code Playgroud)
ContinuousList:
template <typename ValueType>
class ContinuousList
{
public:
ValueType* iterate(std::string& key)
{
m_curr = m_collection.begin(); // Error occurs here
...
}
private:
LinkedList<Node<ValueType>*> m_collection;
Node<ValueType>* m_curr;
};
Run Code Online (Sandbox Code Playgroud)
完整的错误消息
1> error C2440: '=' : cannot convert from 'Node<ValueType> *' to 'Node<ValueType> *'
1> with
1> [
1> ValueType=Node<bool> *
1> ]
1> and
1> [
1> ValueType=bool
1> ]
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1> while compiling class template member function 'bool *ContinuousList<ValueType>::iterate(std::string &)'
1> with
1> [
1> ValueType=bool
1> ]
1> see reference to class template instantiation 'ContinuousList<ValueType>' being compiled
1> with
1> [
1> ValueType=bool
1> ]
Run Code Online (Sandbox Code Playgroud)
LinkedList<Node<ValueType>*> m_collection;
Run Code Online (Sandbox Code Playgroud)
这个
让m_head成为
Node<Node<ValueType>*>*
Run Code Online (Sandbox Code Playgroud)
这不是你想要的.
m_curr = m_collection.begin()
Node<ValueType> = Node<Node<ValueType>*>*
Run Code Online (Sandbox Code Playgroud)
如果
Node<Node<ValueType>*>*
Run Code Online (Sandbox Code Playgroud)
是你想要的,使用
m_collection.begin() - > m_value;
或使用
LinkedList<ValueType>,
Run Code Online (Sandbox Code Playgroud)
它将返回Node
虽然我可能真的很累.... = D.
| 归档时间: |
|
| 查看次数: |
1559 次 |
| 最近记录: |