由模板类型转换的链表错误
Linked list by template type conversion error
我刚刚做了一些关于链表的功能,而使用模板函数它可以处理int和string类型的列表。当我想在指定位置插入一个值(例如0,5)时,在第0个位置插入值为5的值。我使用val_transport来传递我想要在链表中插入的值,并使用模板函数中的T*head来从头部遍历到我想要插入指定值的位置之前的节点。
int add_pos, add_int;
string add_str;
void main_function_insert_at()
{
cout << "Enter the position you want to addnode and its value :";
if (type)
{
node < int> val_transport;
cin >> add_pos >> add_int;
val_transport.value = add_int;
int_head = insert_at(int_head, add_pos, val_transport);
}
else
{
node <string> val_transport;
cin >> add_pos >> add_str;
val_transport.value = add_str;
str_head = insert_at(str_head, add_pos, val_transport);
}
}
template < typename T >
T* insert_at(T* head, int pos, T val_transport)
{
T* cur = head;
for (int i = 0; i < pos - 1; i++)
{
cur = cur->next;
}
if (type)
{
node <int>*newnode=NULL;
newnode = new node < int > ;
newnode->value = val_transport.value;
cur->next = newnode;
newnode->next = cur->next->next;
}
else
{
node <string>*newnode=NULL;
newnode = new node < string > ;
newnode->value = val_transport.value;
cur->next = newnode;
newnode->next = cur->next->next;
}
}
然而,在我的模板函数中,错误像在
一行中不断显示错误4:'=':无法从'node *'转换为'node *'错误6:'=':无法从'node *'转换为'node *'
错误3错误C2440: '=':无法从'node *'转换为'node *'
错误7错误C2440: '=':无法从'node *'转换为'node *'
C2440: '=':无法从'std::string'转换为'int'
他们似乎是同样的问题。
我认为模板可以接受其他类型,如int和string。但很明显,我错了。有人知道吗?谢谢你。
**很抱歉我的英语不好,因为我是新来CS的,来自台湾:p
模板在编译过程中实例化。
假设你有
node<int>* int_head;
node<string>* str_head;
如果你插入一个string
:
,你的模板将实例化为这个node<string>* insert_at(node<string>* head, int pos, node<string> val_transport)
{
node<string>* cur = head;
for (int i = 0; i < pos - 1; i++)
{
cur = cur->next;
}
if (type)
{
node <int>*newnode=NULL;
newnode = new node < int > ;
newnode->value = val_transport.value;
cur->next = newnode;
newnode->next = cur->next->next;
}
else
{
node <string>*newnode=NULL;
newnode = new node < string > ;
newnode->value = val_transport.value;
cur->next = newnode;
newnode->next = cur->next->next;
}
}
可以看到,"true"分支创建了一个类型错误的节点。
您不需要检查应该创建的节点类型-它与模板参数T
:
template <typename T>
T* insert_at(T* head, int pos, T val_transport)
{
T* cur = head;
for (int i = 0; i < pos - 1; i++)
{
cur = cur->next;
}
T *newnode = new T;
newnode->value = val_transport.value;
newnode->next = cur->next;
cur->next = newnode;
return head;
}
你也可以根据列表内容的类型而不是列表本身的类型来编写模板。
这使得使用一个节点只是为了"传输"变得不必要。
template <typename T>
node<T>* insert_at(node<T>* head, int pos, T value)
{
node<T>* cur = head;
for (int i = 0; i < pos - 1; i++)
{
cur = cur->next;
}
node<T> *newnode = new node<T>;
newnode->value = value;
newnode->next = cur->next;
cur->next = newnode;
return head;
}
void main_function_insert_at()
{
cout << "Enter the position you want to add node and its value :";
if (type)
{
cin >> add_pos >> add_int;
int_head = insert_at(int_head, add_pos, add_int);
}
else
{
cin >> add_pos >> add_str;
str_head = insert_at(str_head, add_pos, add_str);
}
}
(您将需要添加一些空列表和越界索引的处理)
相关文章:
- 错误:需要类名(链表c++)
- 链表错误的中间
- 奇怪的C++链表错误
- C++:按数字排序链表错误
- 双向链表错误
- 第一次运行程序时出现损坏的双链表错误,后续运行正常
- c++:链表错误" Access violation reading location"
- 链表错误
- 链表错误无限循环
- C++glibc检测到损坏的双链表错误C++
- 合并两个排序的单向链表 [错误]
- 将对象添加到链表错误 [C++]
- 链表错误:语法错误:标识符'head'之前缺少';'
- 带有队列LNK2005的链表错误
- 打印链表错误。怎么了?
- 有序链表错误
- 从开始内存中删除链表错误
- 由模板类型转换的链表错误
- 双链表错误实现Deque
- 机器相关的损坏的双链表错误