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 8602285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:02:52+00:00 2026-06-12T02:02:52+00:00

I’m trying to use an ANSI C++ for_each statement to iterate over and print

  • 0

I’m trying to use an ANSI C++ for_each statement to iterate over and print the elements of a standard vector. It works if I have the for_each call a non-overloaded function, but yields a compiler error if I have it call an overloaded function.

Here’s a minimal test program to show where the compiler error occurs:

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

struct S {
    char c;
    int i;
};
std::vector<S> v;

void print_struct(int idx);
void print_struct(const struct S& s);

// f: a non-overloaded version of the preceding function.
void f(const struct S& s);

int main()
{
    v.push_back((struct S){'a', 1});
    v.push_back((struct S){'b', 2});
    v.push_back((struct S){'c', 3});

    for (unsigned int i = 0; i < v.size(); ++i)
        print_struct(i);

    /* ERROR! */
    std::for_each(v.begin(), v.end(), print_struct);

    /* WORKAROUND: */
    std::for_each(v.begin(), v.end(), f);

    return 0;
}

// print_struct: Print a struct by its index in vector v.
void print_struct(int idx)
{
    std::cout << v[idx].c << ',' << v[idx].i << '\n';
}

// print_struct: Print a struct by reference.
void print_struct(const struct S& s)
{
    std::cout << s.c << ',' << s.i << '\n';
}

// f: a non-overloaded version of the preceding function.
void f(const struct S& s)
{
    std::cout << s.c << ',' << s.i << '\n';
}

I compiled this in openSUSE 12.2 using:

g++-4.7 -ansi -Wall for_each.cpp -o for_each

The full error message is:

for_each.cpp: In function ‘int main()’:
for_each.cpp:31:48: error: no matching function for call to ‘for_each(std::vector<S>::iterator, std::vector<S>::iterator, <unresolved overloaded function type>)’
for_each.cpp:31:48: note: candidate is:
In file included from /usr/include/c++/4.7/algorithm:63:0,
                 from for_each.cpp:5:
/usr/include/c++/4.7/bits/stl_algo.h:4436:5: note: template<class _IIter, class _Funct> _Funct std::for_each(_IIter, _IIter, _Funct)
/usr/include/c++/4.7/bits/stl_algo.h:4436:5: note:   template argument deduction/substitution failed:
for_each.cpp:31:48: note:   couldn't deduce template parameter ‘_Funct’

I don’t see any search results for this particular error on Stack Overflow, or on the web generally. Any help would be appreciated.

  • 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-12T02:02:53+00:00Added an answer on June 12, 2026 at 2:02 am

    A names refers to an overload set. You’ll need to specify which overload you want:

    std::for_each(v.begin(), v.end(), (void (&)(S const&)) print_struct);
    

    Another approach is to use a polymorphic callable function object as a helper:

    struct PrintStruct
    {
        template <typename T> void operator()(T const& v) const 
            { return print_struct(v); }
    };
    
    int main()
    {
        PrintStruct helper;
    
        std::vector<S> sv;
        std::vector<int> iv;
    
        // helper works for both:
        std::for_each(sv.begin(), sv.end(), helper);
        std::for_each(iv.begin(), iv.end(), helper);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,

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.