mercredi 9 août 2017

No matching function for call to find in templated linked list

Complier error "No matching function for call to" in my case a find function in a linked list.

Why this error?

Function

template<class T>
int find(std::shared_ptr<Node<T>> ptr, T val) {
    int pos(0);
    while (ptr) {
        if (ptr->val == val)
            return pos;
        else {
            ptr = ptr->next;
            pos++;
        }
    }
    return -1; // Not found
}

Invocation

std::cout << "Pos: " << find(myFloat, 9.83) << std::endl;

myFloat is a shared_ptr, and root node for a list of floats, successfully populated as follows:

Linked List of Floats

2.5 ⟹ 3.7 ⟹ 4.8 ⟹ 7.93 ⟹ 0.96 ⟹ 9.83 ⟹ 7.45

Struct

template<class T>
struct Node {
    Node(T k):val(k){}
    T val;
    std::shared_ptr<Node<T>> next = nullptr;
};

Error

No matching function for call to 'find'

Aucun commentaire:

Enregistrer un commentaire