2017-01-23 64 views
-1

在下面的代碼中,我製作了<algorithm>(我相信)中定義的for_each函數的克隆。唯一的問題是第三個參數是我做的一個void函數,我得到了沒有匹配的函數調用....未解決的重載函數類型。有人可以解釋一下這個問題嗎?無法解析的重載函數類型,克隆for_each

#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; 
} 
+9

這就是爲什麼你或不使用'使用命名空間std;'(http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-壞的做法)。你與'std :: fill'和你自己的'fill'衝突。 – NathanOliver

+0

什麼是**精確**錯誤信息和哪一行導致它? –

+0

另外,以'std :: function'作爲參數有什麼問題? –

回答

相關問題