C++ 按模板类型转换的链表错误
在使用模板函数时,我刚刚完成了一些关于链表的功能 s、 它可以处理int和string-like列表。 当我想在指定位置(例如0,5)插入值时,请在第0个位置插入值5。 我使用val_transport传递我要在链表中插入的值,并在模板函数中使用T*head从head遍历到我要插入指定值的位置之前的节点C++ 按模板类型转换的链表错误,c++,templates,data-structures,linked-list,C++,Templates,Data Structures,Linked List,在使用模板函数时,我刚刚完成了一些关于链表的功能 s、 它可以处理int和string-like列表。 当我想在指定位置(例如0,5)插入值时,请在第0个位置插入值5。 我使用val_transport传递我要在链表中插入的值,并在模板函数中使用T*head从head遍历到我要插入指定值的位置之前的节点 int add_pos, add_int; string add_str; void main_function_insert_at() { cout << "Enter
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;
}
}
int添加位置,添加int;
字符串add_str;
void main_函数_insert_at()
{
cout val_运输公司;
cin>>添加位置>>添加int;
val_transport.value=add_int;
int_head=插入(int_head、add_pos、val_transport);
}
其他的
{
节点VALU传输;
cin>>添加位置>>添加位置;
val_transport.value=add_str;
str_head=插入(str_head,add_pos,val_transport);
}
}
模板
T*插入位置(T*头部、内部位置、T val\U运输)
{
T*cur=头部;
对于(int i=0;inext;
}
如果(类型)
{
node*newnode=NULL;
newnode=新节点;
newnode->value=val\u transport.value;
cur->next=newnode;
新建节点->下一步=当前->下一步->下一步;
}
其他的
{
node*newnode=NULL;
newnode=新节点;
newnode->value=val\u transport.value;
cur->next=newnode;
新建节点->下一步=当前->下一步->下一步;
}
}
但是,在我的模板函数中,错误如下
是不断显示在一行又一行,如
错误4错误C2440:“=”:无法从“节点*”转换为“节点*”
错误6错误C2440:“=”:无法从“节点*”转换为“节点*”错误3错误C2440:“=”:无法从“节点*”转换为“节点*”
错误7错误C2440:“=”:无法从“节点*”转换为“节点*”
错误5错误C2440:“=”:无法从“std::string”转换为“int” 它们似乎是相同的问题 我认为模板一可以接受其他类型,如int和string。 但很明显,我错了。 有人有这个想法吗?非常感谢
**很抱歉我的英语不好,因为我是CS的新手,来自台湾:p模板在编译期间实例化 假设你有
node<int>* int_head;
node<string>* str_head;
如您所见,“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;
}
模板
T*插入位置(T*头部、内部位置、T val\U运输)
{
T*cur=头部;
对于(int i=0;inext;
}
T*newnode=newt;
newnode->value=val\u transport.value;
新建节点->下一步=当前->下一步;
cur->next=newnode;
回流头;
}
您还可以根据列表内容的类型而不是列表本身的类型编写模板。这使得仅为“传输”而使用节点变得不必要
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);
}
}
模板
节点*插入位置(节点*头部、内部位置、T值)
{
节点*cur=头部;
对于(int i=0;inext;
}
node*newnode=新节点;
新建节点->值=值;
新建节点->下一步=当前->下一步;
cur->next=newnode;
回流头;
}
void main_函数_insert_at()
{
cout>add\u pos>>add\u int;
int_head=插入(int_head,add_pos,add_int);
}
其他的
{
cin>>添加位置>>添加位置;
str_head=插入(str_head,add_pos,add_str);
}
}
(您需要添加一些空列表处理和越界索引。)欢迎使用SO。请看。最好发布一个。您不能在运行时为列表选择一个类型。@molbdnilo我尝试了运算符重载,但也失败了,是否存在其他选项,谢谢:Dthanks非常感谢:D它可以工作,但是模板节点*和模板
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);
}
}