转换为模板类(节点)
Converting to template class (NODES)
template <class Type>
class Node
{
public:
Node ()
{
}
Node (Type x, Node* nd)
{
data = x;
next = nd;
}
Node (Type x)
{
data = x;
next = NULL;
}
~Node (void)
{
}
Node (const Node* & nd)
{
data = nd->data;
next = nd->next;
}
Node & Node::operator = (const Node* & nd)
{
data = nd->data;
next = nd->next;
}
T data;
Node* next;
};
我是否将每个节点* 替换为
Node*<Type>
我尝试更换它并尝试运行类似的东西
Node* temp = myq.head;
但它说类模板"节点"的参数列表丢失。当我需要 Node 类本身成为模板的一部分时,我不确定如何使用模板
Node 的每个声明都需要一个 <> 类型。
为
Node* temp = myq.head;
这取决于myq.head的定义。如果它被定义为 Node<int>*,那么 temp 也必须定义为 Node<int>* temp。您始终必须具有模板对象的<>。
如果你想在不知道类型的情况下拥有 Node*,你可以使用继承。具有从非模板 Node 类继承的模板化 TypedNode 类。您将能够使用 Node* 传递所有这些 TypeNode<> 对象,但在不知道节点类型的情况下,您将无法获取节点的值。
我不建议这样做,但是如果您真的想制作具有混合类型的节点列表,则需要通过以下方式跟踪类型
- 在基类中包含一个枚举类型,该类型定义存储在节点中的类型,并为每个类定义 typedNode,在其构造函数中设置枚举,或从虚拟方法返回枚举。
- RTTI,运行时类型信息 http://en.wikipedia.org/wiki/Run-time_type_information
相关文章:
- 如何将节点 V8 字符串转换为 C++ 字符串
- 没有从"树节点"到"树节点*"的可行转换
- 将节点数组(可变长度)转换为常量浮点**以调用opencv.calcHist
- 正在初始化:无法从'_Ty*'转换为 List<int,std::分配器<节点<T>>>:节点*
- 错误:无法从节点*转换为节点 c++?
- 指针取消引用:无法将“节点**”转换为“节点*”
- 无法将参数从"this"转换为节点<T>*
- 用指针 *将C 转换为JavaScript(节点)
- 如何将XML节点数据转换为QT中的字符串
- 如何将节点插件持久<function>转换为本地<function>?
- 在从通用树转换的二进制树中查找节点的父
- 如何使用指针C++将类的析构函数转换为另一个类?类似于单向列表容器类和节点类
- 将阵列转换为节点
- 转换为模板类(节点)
- 如何将列表'node_ptr'转换为我自己的节点类型
- 错误 C2440:"=":无法从"列表节点 *"<T>转换为"列表节点<T> *"
- 在C++中实现图形节点时将 C# 类转换为C++
- 无法在赋值中将"列表节点**"转换为"列表节点*"
- 用于c++的XML解析器,它将XML节点转换为c++对象
- C++中的树实现:无法将节点转换为节点*