按模板类型转换错误

时间:2016-09-27 05:57:47

标签: c++ templates data-structures linked-list

我在使用模板功能时刚刚完成了一些关于链表的功能 S.T.它可以处理int和string喜欢的列表。 当我想在指定位置插入一个值(ex 0,5)时,插入第5个位置,值为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错误C2440:'=':无法从'node *'转换为'node *' 错误6错误C2440:'=':无法从'node *'转换为'node *'
错误3错误C2440:'=':无法从'node *'转换为'node *'
错误7错误C2440:'=':无法从'node *'转换为'node *'
错误5错误C2440:'=':无法从'std :: string'转换为'int'

他们似乎也是同样的问题。

我认为模板可以接受另一种类型,例如int和string。 但显然,我错了。 有没有人有这个想法?非常感谢

**因为我不熟悉CS和来自台湾,所以我的英语不好:P

1 个答案:

答案 0 :(得分:0)

在编译期间实例化模板。

假设你有

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);
    }
}

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