小编Lav*_*nti的帖子

C++模板方法:为什么Node <int>没问题,但Node <float>?

我很少使用模板.我不知道为什么我看到下面的代码生成错误的push方法Node<float>

构建错误是:没有匹配函数来调用push.

Node<int>* push方法很好.

Node<float>* head1 = NULL;
push(&head1, 1);

template <typename T>
struct Node {
    T data;
    Node* next;
};

template <typename T>
void push(Node<T>** head, T data) {
    Node<T>* tmp = *head;
    Node<T>* newNode = NULL; //createNode(data);

    if (tmp == NULL) {
        *head = newNode;
    }
    else {
        while (tmp->next)
            tmp=tmp->next;

        tmp->next = newNode;
    }
}

int main(int argc, const char * argv[]) {
    Node<float>* head1 = NULL;
    push(&head1, 1);

    Node<int>* head = NULL;
    push(&head, …
Run Code Online (Sandbox Code Playgroud)

c++ templates

15
推荐指数
3
解决办法
1538
查看次数

标签 统计

c++ ×1

templates ×1