-1

In the following code I made a clone of an for_each function defined in <algorithm> (I believe). The only problem is for the third argument which is a void function I made, I get the no matching function for call....unresolved overloaded function type. Could someone shed some light on this matter?

#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;
}
drescherjm
  • 10,365
  • 5
  • 44
  • 64
monolith937
  • 429
  • 1
  • 4
  • 13
  • 9
    And this is why you [do not use `using namespace std;`](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). your conflicting with `std::fill` and your own `fill`. – NathanOliver Jan 23 '17 at 13:24
  • What is the **exact** error message and which line causes it? – Code-Apprentice Jan 23 '17 at 13:24
  • In addition, what's wrong with taking `std::function` as an argument? – Algirdas Preidžius Jan 23 '17 at 13:25
  • @NathanOliver If I could upvote you more than once, I would have already done so. – Borgleader Jan 23 '17 at 13:25
  • @Borgleader Thanks. Think it should be an answer or just close vote as a typo, unlikely to help others? – NathanOliver Jan 23 '17 at 13:26
  • @AlgirdasPreidžius What would be the advantage of taking std::function in this case? AFAIK the general use case is for storing a functor of some sort. No need to do that here. – Borgleader Jan 23 '17 at 13:26
  • Yes it appears there was an already defined `fill` method inside std. I was taught (wrongly apparently) to use `using namespace` to shorten typing in general. – monolith937 Jan 23 '17 at 13:29
  • @Borgleader Well, that was the first thought when looking at the code, since the signature for calling said functor is fixed, within `clone_for_each`, so you would, at least, declare what signature you expect from the functor up front, rather than forcing the potential user to look at the implementation. – Algirdas Preidžius Jan 23 '17 at 13:29
  • 1
    @monolith937 It is a very bad habit that gets taught to a lot of people. The sooner you get burned by it the better because then you'll stop using it. writing `std::` all over the place isn't so bad when it makes you code actually work :-) – NathanOliver Jan 23 '17 at 13:33
  • @monolith937 - shortening typing is not a design goal. – Pete Becker Jan 23 '17 at 13:57

1 Answers1

4

Since you're using namespace std, fill from

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

is supposed to be std::fill() from <algorithm>and don't fit to clone_for_each().

See Why using namespace std is considered harmful?

Community
  • 1
  • 1
YSC
  • 38,212
  • 9
  • 96
  • 149