friend classes

Discussion in 'C++' started by Srini nandiraju, May 19, 2004.

  1. Hi folks,

    I am trying to implement a friend class and the following is what I did.
    Please scroll down.

    /***************** CODE **********************************/
    class BKP
    {
    private:
    int nVar;
    vector<int> p;
    vector<int> w;
    int b;

    public:

    BKP();
    BKP(ifstream &in);
    ~BKP();

    friend ostream &operator<<( ostream &_os, BKP& _bkp);

    };

    ostream& operator <<( ostream& _os, BKP& _bkp)
    {

    for ( int i =0; i <_bkp.nVar ; i++)
    {
    _os << _bkp.p.at(i) << " " << _bkp.w.at(i) << endl;
    }
    _os << _bkp.b <<endl;

    return _os;
    }


    I tried to compile in MSVC++ 6.0 and I get the following errors:

    error C2248: 'nVar' : cannot access private member declared in class 'BKP'
    error C2248: 'p' : cannot access private member declared in class 'BKP'
    error C2248: 'w' : cannot access private member declared in class 'BKP'
    error C2248: 'b': cannot access private member declared in class 'BKP'

    I can't figure this. A friend function is supposed to access the private
    members is what I read. Please clarify. thanx for ur time.

    srini.
     
    Srini nandiraju, May 19, 2004
    #1
    1. Advertising

  2. Srini nandiraju wrote in news:b6da7d0d.0405181949.1d79f9f3
    @posting.google.com in comp.lang.c++:

    > I tried to compile in MSVC++ 6.0 and I get the following errors:
    >
    >


    MSVC++ 6.0 is your problem, upgrade your compiler. MSVC 7.1 is the
    current version if you can live without the IDE its a free download.

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, May 19, 2004
    #2
    1. Advertising

  3. Srini nandiraju

    David White Guest

    "Srini nandiraju" <> wrote in message
    news:...
    > Hi folks,
    >
    > I am trying to implement a friend class and the following is what I did.
    > Please scroll down.
    >
    > /***************** CODE **********************************/
    > class BKP
    > {
    > private:
    > int nVar;
    > vector<int> p;
    > vector<int> w;
    > int b;
    >
    > public:
    >
    > BKP();
    > BKP(ifstream &in);
    > ~BKP();
    >
    > friend ostream &operator<<( ostream &_os, BKP& _bkp);
    >
    > };
    >
    > ostream& operator <<( ostream& _os, BKP& _bkp)
    > {
    >
    > for ( int i =0; i <_bkp.nVar ; i++)
    > {
    > _os << _bkp.p.at(i) << " " << _bkp.w.at(i) << endl;
    > }
    > _os << _bkp.b <<endl;
    >
    > return _os;
    > }
    >
    >
    > I tried to compile in MSVC++ 6.0 and I get the following errors:
    >
    > error C2248: 'nVar' : cannot access private member declared in class 'BKP'
    > error C2248: 'p' : cannot access private member declared in class 'BKP'
    > error C2248: 'w' : cannot access private member declared in class 'BKP'
    > error C2248: 'b': cannot access private member declared in class 'BKP'
    >
    > I can't figure this. A friend function is supposed to access the private
    > members is what I read. Please clarify. thanx for ur time.


    That's not what I get when I compile it with VC++ 6.0. I get:
    z.cpp(5) : error C2143: syntax error : missing ';' before '<'
    z.cpp(5) : error C2501: 'vector' : missing storage-class or type specifiers
    z.cpp(5) : error C2059: syntax error : '<'
    z.cpp(5) : error C2238: unexpected token(s) preceding ';'
    z.cpp(6) : error C2143: syntax error : missing ';' before '<'
    And so on and on...

    The reason is that you omitted the following:
    #include <ostream>
    #include <vector>

    You also did not include the "std::" qualifier on anything. Without them you
    need to use 'using' at least once.

    The following compiles without an error, though I don't know why it doesn't
    complain about ifstream:
    #include <ostream>
    #include <vector>

    using namespace std;

    class BKP
    {
    private:
    int nVar;
    vector<int> p;
    vector<int> w;
    int b;

    public:

    BKP();
    BKP(ifstream &in);
    ~BKP();

    friend ostream &operator<<( ostream &_os, BKP& _bkp);

    };

    ostream& operator <<( ostream& _os, BKP& _bkp)
    {

    for ( int i =0; i <_bkp.nVar ; i++)
    {
    _os << _bkp.p.at(i) << " " << _bkp.w.at(i) << endl;
    }
    _os << _bkp.b <<endl;

    return _os;
    }

    DW
     
    David White, May 19, 2004
    #3
  4. Srini,



    I think what you are seeing is a VC++ 6 bug, I used the .NET

    2003 C++ and the code below works ok and prints out what you'd

    expect.



    dave



    +++++++++++++++++++++++++++++++++++++++++++



    #include <vector>

    #include <iostream>

    using namespace std;

    class Matrix;

    class Vector

    {

    public:

    float v[4];

    friend Vector operator*(const Matrix& M, const Vector& V);

    };

    class Matrix

    {

    private:

    Vector v[4];

    public:

    friend ostream;

    friend ostream& operator<<( ostream& _os, const Matrix& M );

    friend Vector operator*(const Matrix& M, const Vector& V);

    };

    ostream& operator<< ( ostream& _os, const Matrix& M)

    {

    for ( int i=0; i<4; i++)

    for (int j=0; j<4; j++)

    {

    float x = M.v.v[j];

    }

    return _os;

    }

    Vector operator*(const Matrix& M, const Vector& V)

    {

    Vector U;

    for (int i=0; i<4; i++)

    {

    U.v=0;

    for (int j=0; j<4; j++)

    {

    U.v += M.v.v[j] * V.v[j];

    cout << U.v;

    }

    }

    return U;

    }





    class BKP

    {


    int nVar;

    vector<int> p;

    vector<int> w;

    int b;

    public:

    BKP()

    {

    nVar=10;

    b=1;

    for (int i=0; i<10; i++)

    {

    p.push_back(i);

    w.push_back(i*i);

    }


    };

    BKP(ifstream &in){};

    ~BKP(){};

    friend ostream& operator<<( ostream& _os, const BKP& _bkp );

    };

    ostream& operator <<( ostream& _os, const BKP& _bkp)

    {

    for ( int i =0; i <_bkp.nVar ; i++)

    {

    _os << _bkp.p.at(i) << " " << _bkp.w.at(i) << endl;

    }

    _os << _bkp.b <<endl;

    return _os;

    }




    int main()

    {

    BKP bkp;

    cout << bkp;

    return 0;

    }

    "Srini nandiraju" <> wrote in message
    news:...
    > Hi folks,
    >
    > I am trying to implement a friend class and the following is what I did.
    > Please scroll down.
    >
    > /***************** CODE **********************************/
    > class BKP
    > {
    > private:
    > int nVar;
    > vector<int> p;
    > vector<int> w;
    > int b;
    >
    > public:
    >
    > BKP();
    > BKP(ifstream &in);
    > ~BKP();
    >
    > friend ostream &operator<<( ostream &_os, BKP& _bkp);
    >
    > };
    >
    > ostream& operator <<( ostream& _os, BKP& _bkp)
    > {
    >
    > for ( int i =0; i <_bkp.nVar ; i++)
    > {
    > _os << _bkp.p.at(i) << " " << _bkp.w.at(i) << endl;
    > }
    > _os << _bkp.b <<endl;
    >
    > return _os;
    > }
    >
    >
    > I tried to compile in MSVC++ 6.0 and I get the following errors:
    >
    > error C2248: 'nVar' : cannot access private member declared in class 'BKP'
    > error C2248: 'p' : cannot access private member declared in class 'BKP'
    > error C2248: 'w' : cannot access private member declared in class 'BKP'
    > error C2248: 'b': cannot access private member declared in class 'BKP'
    >
    > I can't figure this. A friend function is supposed to access the private
    > members is what I read. Please clarify. thanx for ur time.
    >
    > srini.
     
    Dave Townsend, May 19, 2004
    #4
  5. "Srini nandiraju" <> wrote in message
    news:...
    > Hi folks,
    >
    > I am trying to implement a friend class and the following is what I did.
    > Please scroll down.
    >
    > /***************** CODE **********************************/
    > class BKP
    > {
    > private:
    > int nVar;
    > vector<int> p;
    > vector<int> w;
    > int b;
    >
    > public:
    >
    > BKP();
    > BKP(ifstream &in);
    > ~BKP();
    >
    > friend ostream &operator<<( ostream &_os, BKP& _bkp);
    >
    > };
    >
    > ostream& operator <<( ostream& _os, BKP& _bkp)
    > {
    >
    > for ( int i =0; i <_bkp.nVar ; i++)
    > {
    > _os << _bkp.p.at(i) << " " << _bkp.w.at(i) << endl;
    > }
    > _os << _bkp.b <<endl;
    >
    > return _os;
    > }
    >
    >
    > I tried to compile in MSVC++ 6.0 and I get the following errors:
    >
    > error C2248: 'nVar' : cannot access private member declared in class 'BKP'
    > error C2248: 'p' : cannot access private member declared in class 'BKP'
    > error C2248: 'w' : cannot access private member declared in class 'BKP'
    > error C2248: 'b': cannot access private member declared in class 'BKP'
    >
    > I can't figure this. A friend function is supposed to access the private
    > members is what I read. Please clarify. thanx for ur time.
    >
    > srini.


    This is an MSVC++ 6 bug. Which service pack are you using? I think this was
    fixed in the latest service pack.

    Another workaround is to move the function into the class

    class BKP
    {
    ...

    friend ostream &operator<<( ostream &_os, const BKP& _bkp)
    {
    ...
    }
    };

    Also you should declare operator<< with a const reference parameter like I
    have done above. operator<< does not change _bkp, so the reference should be
    const.

    john
     
    John Harrison, May 19, 2004
    #5
  6. Srini nandiraju

    David White Guest

    In light of John Harrison's post, I'll add that I'm using Service Pack 5 for
    my VC++ 6.0.

    DW
     
    David White, May 19, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. z f
    Replies:
    0
    Views:
    299
  2. Alexander Stippler

    nested classes and friend declaration

    Alexander Stippler, Jul 2, 2003, in forum: C++
    Replies:
    1
    Views:
    383
    Victor Bazarov
    Jul 2, 2003
  3. Adam Parkin
    Replies:
    6
    Views:
    519
    Victor Bazarov
    Apr 24, 2004
  4. Snyke

    What are friend classes?

    Snyke, Aug 5, 2004, in forum: C++
    Replies:
    2
    Views:
    297
    rossum
    Aug 5, 2004
  5. Peter
    Replies:
    2
    Views:
    275
    Öö Tiib
    Jun 6, 2013
Loading...

Share This Page