-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;
}
这就是为什么你或不使用'使用命名空间std;'(http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-坏的做法)。你与'std :: fill'和你自己的'fill'冲突。 – NathanOliver
什么是**精确**错误信息和哪一行导致它? –
另外,以'std :: function'作为参数有什么问题? –