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[i].v[j];
}
return _os;
}
Vector operator*(const Matrix& M, const Vector& V)
{
Vector U;
for (int i=0; i<4; i++)
{
U.v[i]=0;
for (int j=0; j<4; j++)
{
U.v[i] += M.v[i].v[j] * V.v[j];
cout << U.v[i];
}
}
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" <na******@ureach.com> wrote in message
news:b6**************************@posting.google.c om...
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.