未解决的重载函数类型,克隆for_each

时间:2017-01-23 13:22:15

标签: c++

在下面的代码中,我复制了for_each中定义的<algorithm>函数(我相信)。唯一的问题是第三个参数是我做的void函数,我得到了调用的无匹配函数....未解析的重载函数类型。有人可以解释一下这个问题吗?

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

void fill(int& n) {          //The custom made function, a simple rewrite,
    if (n < 100)             //which is why I passed an int reference
        n = 100;
}

template <class Iterator, class Function>            //Clone for_each
void clone_for_each(Iterator first, Iterator last, Function f) {
    while( first != last) {
        f(*first);
        first++;
    }
}

int main (int argc, char const* argv[])
{
    //Just inputing data and printing it out
    //This part is fine up until...
    int n;
    cout << "Unesite broj vrsta artikala: ";
    cin >> n;

    vector<string> names;
    vector<int> quantity;

    cout << "Unesite naziv artikla potom njegovu kolicinu: " << endl;
    for (int i = 0; i < n; i++) {
        string name;
        int amount;

        cout << "Unesite naziv: ";
        cin >> name;
        cout << endl;
        cout << "Unesite kolicinu: ";
        cin >> amount;
        cout << endl;

        names.push_back(name);
        quantity.push_back(amount);
    }

    cout << "Raspolozivi artikli: " << endl;
    vector<string>::iterator itNames = names.begin();
    vector<int>::iterator itQuantity = quantity.begin();


    for(itNames, itQuantity; itNames != names.end(), itQuantity != quantity.end(); itNames++, itQuantity++ ) 
        cout << *itNames << " " << *itQuantity << endl;
    cout << "Artikli nakon dopune: " << endl;

    //right here, which is where I called for clone_for_each
    clone_for_each(quantity.begin(), quantity.end(), fill);  

    return 0;
}

1 个答案:

答案 0 :(得分:4)

由于您是using namespace stdfill来自

clone_for_each(quantity.begin(), quantity.end(), fill);

应该是来自<algorithm>的{​​{3}},并且不适合clone_for_each()

请参阅std::fill()