由模板类型转换的链表错误

Linked list by template type conversion error

本文关键字:链表 错误 类型转换      更新时间:2023-10-16

我刚刚做了一些关于链表的功能,而使用模板函数它可以处理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);
    }
}

(您将需要添加一些空列表和越界索引的处理)