Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8872599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:15:27+00:00 2026-06-14T18:15:27+00:00

here is the code: #include <vector> #include <algorithm> #include <string> #include <map> #include <iostream>

  • 0

here is the code:

#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <iostream>
using namespace std;

map<string, int> g_map;
void read_item(const pair<string, int>& p) {
   g_map[p.first] += p.second;
}

void myprint(std::pair<const string, int> ci) {
   cout << "first : " << ci.first << "seconde : " << ci.second << endl;
}

void myprint(int ci) {
   cout << ci << endl;
}

int main()
{
    string a = string("nail");
    string b = string("hammer");
    read_item(make_pair(a, 100));
    read_item(make_pair(b, 2));
    read_item(make_pair(b, 10));
    read_item(make_pair(a, 200));

    std::for_each(g_map.begin(), g_map.end(), myprint); // can't find the matching function here
    vector<int> vec;
    vec.push_back(3);
    vec.push_back(3);
    std::for_each(vec.begin(), vec.end(), myprint);  // and here
    return 0;
}

I overloaded the function myprint, I think it should be working, but it doesn’t. when I change the first myprint to myprint1 and the second to myprint2, it works. any body can help? the compile error is:

funcTemOverload.cpp: In function 'int main()':
funcTemOverload.cpp:29:54: error: no matching function for call to 'for_each(std::map<std::basic_string<char>, int>::iterator, std::map<std::basic_string<char>, int>::iterator, <unresolved overloaded function type>)'
funcTemOverload.cpp:29:54: note: candidate is:
In file included from d:\mingw\bin\../lib/gcc/x86_64-w64-mingw32/4.7.2/include/c++/algorithm:63:0,
         from funcTemOverload.cpp:2:
d:\mingw\bin\../lib/gcc/x86_64-w64-mingw32/4.7.2/include/c++/bits/stl_algo.h:4436:5: note: template<class _IIter, class _Funct> _Funct std::for_each(_IIter, _IIter, _Funct)
d:\mingw\bin\../lib/gcc/x86_64-w64-mingw32/4.7.2/include/c++/bits/stl_algo.h:4436:5: note:   template argument deduction/substitution failed:
funcTemOverload.cpp:29:54: note:   couldn't deduce template parameter '_Funct'
funcTemOverload.cpp:35:50: error: no matching function for call to 'for_each(std::vector<int>::iterator, std::vector<int>::iterator, <unresolved overloaded function type>)'
funcTemOverload.cpp:35:50: note: candidate is:
In file included from d:\mingw\bin\../lib/gcc/x86_64-w64-mingw32/4.7.2/include/c++/algorithm:63:0,
         from funcTemOverload.cpp:2:
d:\mingw\bin\../lib/gcc/x86_64-w64-mingw32/4.7.2/include/c++/bits/stl_algo.h:4436:5: note: template<class _IIter, class _Funct> _Funct std::for_each(_IIter, _IIter, _Funct)
d:\mingw\bin\../lib/gcc/x86_64-w64-mingw32/4.7.2/include/c++/bits/stl_algo.h:4436:5: note:   template argument deduction/substitution failed:
funcTemOverload.cpp:35:50: note:   couldn't deduce template parameter '_Funct'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:15:28+00:00Added an answer on June 14, 2026 at 6:15 pm

    As requested, the reason that your original code fails is that the UnaryFunction template parameter of std::for_each is only used in a “non-deduced context”, and is not explicitly specified. [temp.deduct.type]/4:

    … If a template parameter is used only in non-deduced contexts and is not explicitly specified, template argument deduction fails.

    UnaryFunction is treated as a non-deduced context because of [temp.deduct.call]/6

    When P [UnaryFunction] is a function type, pointer to function type, or pointer to member function type … If the argument [myprint] is an overload set (not containing function templates), trial argument deduction is attempted using each of the members of the set. If deduction succeeds for only one of the overload set members, that member is used as the argument value for the deduction. If deduction succeeds for more than one member of the overload set the parameter is treated as a non-deduced context.

    One option is to add casts:

    std::for_each(
        g_map.begin(), g_map.end(),
        static_cast<void (*)(std::pair<const string, int>)>(myprint));
    
    std::for_each(
        vec.begin(), vec.end(),
        static_cast<void (*)(int)>(myprint));
    

    Another option is to make myprint be a function object, so that overload resolution occurs inside std::for_each:

    struct myprint {
        void operator()(std::pair<const string, int> ci) const {
            cout << "first : " << ci.first << "seconde : " << ci.second << endl;
        }
        void operator()(int ci) const {
            cout << ci << endl;
        }
    };
    
    std::for_each(g_map.begin(), g_map.end(), myprint());
    std::for_each(vec.begin(), vec.end(), myprint());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's my code so far: #include<iostream> #include<string> #include<fstream> using namespace std; int main() {
here is code for regular expression matching #include<iostream> #include<stdio.h> #include<string.h> using namespace std; int
Here is the code: #include <string> #include <iostream> #include <vector> #include <algorithm> using namespace
Here is my code: #include <iostream> #include <stdlib.h> #include <fstream> using namespace std; int
#include <boost/algorithm/string.hpp> #include <vector> #include <iostream> #include <string> using namespace std; using namespace boost;
here is code #include <iostream> #include <string> #include <algorithm> #include <vector> #include <fstream> #include
The simplest code is the best asker: #include <vector> #include <algorithm> using namespace std;
Here is the code for sorting a vector of string s #include<iostream> #include <vector>
I have the following code #include <algorithm> #include <iostream> #include <vector> #include <functional> int
I'm testing the following code: #include <iostream> #include <vector> #include <algorithm> #include <ctime> int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.