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

  • Home
  • SEARCH
  • 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 711629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:45:25+00:00 2026-05-14T04:45:25+00:00

I have a abstract base class A and a set of 10 derived classes.

  • 0

I have a abstract base class A and a set of 10 derived classes. The infix operator is overloaded in all of the derived classes

class A{
 public:
    void printNode( std::ostream& os )
    {
           this->printNode_p();
    } 
  protected:
    virtual void printNode_p( std::ostream& os )
    {
           os << (*this);
    }
};

There is a container which stores the base class pointers. I want to use boost::bind function to call the overloaded infix operator in each of its derived class. I have written like this

std::vector<A*> m_args
....
std::ostream os;
for_each( m_args.begin(), m_args.end(), bind(&A::printNode, _1, os) );

What is the problem with this code? In visual studio i am getting an error like this

error C2248:
‘std::basic_ios<_Elem,_Traits>::basic_ios’
: cannot access private member
declared in class
‘std::basic_ios<_Elem,_Traits>’

Thanks,
Gokul.

  • 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-14T04:45:25+00:00Added an answer on May 14, 2026 at 4:45 am

    Consider this, which works as expected:

    #include <iostream>
    
    struct base
    {
        virtual ~base(void) {}
    
        virtual void print(std::ostream& pStream) = 0;
    };
    
    struct foo : base
    {
        void print(std::ostream& pStream) { pStream << "foo" << std::endl; }
    };
    
    struct bar : base
    {
        void print(std::ostream& pStream) { pStream << "bar" << std::endl; }
    };
    
    #include <boost/bind.hpp>
    #include <boost/ptr_container/ptr_vector.hpp>
    #include <algorithm>
    
    int main(void)
    {
        boost::ptr_vector<base> v;
        v.push_back(new foo);
        v.push_back(new bar);
    
        std::for_each(v.begin(), v.end(),
                      boost::bind(&base::print, _1, boost::ref(std::cout)));
    }
    

    First off, since you’re using boost you may as well use ptr_vector to handle memory management for you. So, that’s in there.

    Secondly, your error is because streams are not copyable; however, boost::bind will make a copy of all it’s arguments when constructing the functor. Wrap it in a boost::reference_wrapper (using the boost::ref utility function), which is copyable. When the time comes, the wrapper will convert to the necessary type and you won’t notice the difference.

    (This is one of the situations boost::ref was made for.)


    That all said, consider using BOOST_FOREACH, which in my opinion generates the cleanest code:

    #include <boost/foreach.hpp>
    #include <boost/ptr_container/ptr_vector.hpp>
    #include <algorithm>
    
    #define foreach BOOST_FOREACH
    
    int main(void)
    {
        boost::ptr_vector<base> v;
        v.push_back(new foo);
        v.push_back(new bar);
    
        foreach (base* b, v)
        {
            v->print(std::cout);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an abstract base class and derived class: type TInterfaceMethod = class public
I have an abstract base class with a TcpClient field: public abstract class ControllerBase
If I have a base class such that public abstract class XMLSubscription <T extends
I have the following base abstract class defined as: public abstract class BaseObject<T> :
I'm trying to have an abstract base class for some builder classes so I
I have a set of classes that inherit from the same base class and
I have an abstract base class which acts as an interface. I have two
I have an abstract base class and I want to declare a field or
I have an abstract base class called Shape from which both Circle and Rectangle
Ok so I have an abstract base class called Product, a KitItem class that

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.