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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:53:30+00:00 2026-05-12T18:53:30+00:00

My disclaimer here is that I started teaching myself C++ about a week ago

  • 0

My disclaimer here is that I started teaching myself C++ about a week ago and my former experience with programming has been with dynamic languages (Python, javascript).

I’m trying to iterate though the contents of a vector using a generic function to print out the items:

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

using std::vector;
using std::cout;

template <class T>
void p(T x){
    cout << x;
}

int main () {

    vector<int> myV;

    for(int i = 0; i < 10; i++){
        myV.push_back(i);
    }

    vector<int>::const_iterator iter = myV.begin();

    for_each(iter, myV.end(), p);

    return 0;
}

The code doesn’t compile. Would someone explain why?

Edit: The compiler error:

error: no matching function for call to 'for_each(_gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<const int, _gnu_norm::vector<int, std::allocator<int> > >, __gnu_debug_def::vector<int, std::allocator<int> > >&, __gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<int, __gnu_norm::vector<int, std::allocator<int> > >, __gnu_debug_def::vector<int, std::allocator<int> > >, <unknown type>)'

Thanks!

  • 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-05-12T18:53:30+00:00Added an answer on May 12, 2026 at 6:53 pm

    Try:

    for_each(myV.begin(), myV.end(), p<int>);
    

    There were two mistakes in your code:

    • The iterators were not the same type
    • The function pointer was not actually a pointer.
      • Normally templated functions can be deduced from there parameters. But in this case you are not actually using it you are passing it (or its address) to a function (thus the normal rules on template function deduction did not work). As the compiler can not deduce which version of the function ‘p’ you need to use you must be explicit.

    There is also a nice output iterator that does this:

    std::copy(myV.begin(),myV.end(), std::ostream_iterator<int>(std::cout));
    

    Also note that very few compilers can optimise code across a function pointer call.
    Though most are able to optimise the call if it is an functor object. Thus the following may have been a viable alternative to a function pointer:

    template<typename T>
    struct P
    {
        void operator()(T const& value) const
        {
            std::cout << value;
        }
    };
    
    ....
    
    for_each(myV.begin(), myV.end(), P<int>());
    

    Another note:
    When you use templated methods/functions it is usually better to pass by const reference than value. If the Type is expensive to copy then passing by value will generate a copy construction which may not be what you expected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Disclaimer - I've only been using C# for about a week now, so hopefully
Disclaimer: I know this type of question has been asked here before, I just
Disclaimer: I am new to python and Django, but have Drupal programming experience. I'm
Disclaimer: this question is strictly about mysql and database abstraction layers that support it.
Disclaimer: Cocoa newbie here. I wrote an app with a Cocoa GUI that acts
Disclaimer Yes, I am fully aware that what I am asking about is totally
Disclaimer: I'm an iOS developer with little JQuery, PHP or MySQL experience. Here's the
Disclaimer: I am new to python and django but have Drupal programming experience. I'm
Disclaimer: I'm a rails n00b. I'm playing around with a simple helper function that
Disclaimer, new to programming, working my way through C++ Prime Plus 6th ed. I'm

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.