jeudi 3 octobre 2019

Function Template not working, getting error "No suitable user-defined conversion"

I am trying to use a container (vector, list, map, etc...) on a function template but I keep getting an error saying "No suitable user-defined conversion exists"

I tried making a different function template, print_container with 1 argument, and it works.

#include "stdafx.h"
#include <iostream>
#include <vector>

template<typename T>
using Iterator = typename T::iterator;

template<typename C, typename V>
std::vector<Iterator<C>> find_all(C& container, V value) {
    std::vector<Iterator<C>> res;
    for (auto p = container.begin(); p != container.end(); ++p)
        if ((*p) == value)
            res.push_back(p);
    return res;
}

int main() {
    std::vector<int> vec1 = { 1, 2, 3 };
    std::vector<Iterator<int>> res = find_all(vec1, 1); // does not work
    return 0;
}

find_all should return a vector of iterators with only 1 iterator, the iterator attached to vec1[0] and assign that vector to res.

Aucun commentaire:

Enregistrer un commentaire