Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

Getting this error and I'm pretty sure it's in the operator<< function. Both prints are public.

void CRational::print() const
{
    print(cout);
}

void CRational::print(ostream & sout) const
{
    if(m_denominator == 1)
        cout << m_numerator;
    else
        cout << m_numerator << "/" << m_denominator;
}

ostream operator<<(ostream & sout,const CRational a)
{
    a.print();

    return sout;
}

CRational operator++() // prefix ++x
{
    m_numerator += m_denominator;
    return *this;
}

in main:
cout << "e before: " << e << ", \"cout << ++e\" : " << ++e << " after: " << e << endl; 
like image 776
andrey Avatar asked Feb 28 '11 03:02

andrey


1 Answers

You need to return the ostream by reference, not value. Its trying to call a constructor. Might as well pass 'a' as reference as well:

ostream& operator<<(ostream & sout,const CRational& a)

I also note that the print method is probably wrong. It has sout passed as the name of the stream but is then implemented using cout directly. It should be

void CRational::print(ostream & sout) const
{
    if(m_denominator == 1)
        sout << m_numerator;
    else
        sout << m_numerator << "/" << m_denominator;
}
like image 118
Keith Avatar answered Oct 29 '22 16:10

Keith