473,320 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

friend classes

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.
Jul 22 '05 #1
5 2392
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/
Jul 22 '05 #2
"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.


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

Jul 22 '05 #3
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.

Jul 22 '05 #4

"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.


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
Jul 22 '05 #5
In light of John Harrison's post, I'll add that I'm using Service Pack 5 for
my VC++ 6.0.

DW

Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Sebastian Faust | last post by:
Hi, is a construction like the following possible: template<class view_model> class template_clase { protected: template_clase() {} virtual ~template_clase() {}
15
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
7
by: Jesper | last post by:
I need to grant a class access to protected fields of another class in the way its possible in C++ with the friend keyword. However I would like to keep the class protected towards other class...
4
by: darrel | last post by:
In the context of VB.net, when/why would one use a 'friend class'. I'm finding lots of references to friend classes on google, but not a simle definition of what they actually are. Actually, if...
2
by: anongroupaccount | last post by:
I have a smaller class that essentially acts as a record for a larger class (stored in a vector inside of the larger class). In a sense, it's splitting functionality over two classes. I don't...
8
by: toton | last post by:
Hi, I have a parser interface , which is class IParser{ public: void readHeader(Document& doc)= 0; void readCC(CC& cc) = 0; }; Now, there are 3 kinds of parser which extends (implements)...
3
by: Neal | last post by:
Hi all Why do I get a compiler error with the following code. Friend Class SomeClass End Class Public Class SomePublicClass Protected Friend Function AMethod As SomeClass
5
by: Amit Gupta | last post by:
How do I do it (either by friend or by any other hack). I have three class, I am just writing derivation below: class A{} class B{} class C : public class B{} class D: public class B ()
19
by: subramanian100in | last post by:
Stroustrup, in his book TC++PL 3rd Edition, in page 16, under the section '1.8 Advice' has mentioned the following: Don't use global data(use members) Don't use global functions Don't use...
1
by: Immortal_Nephi | last post by:
I want to show you an example how two classes can have relationship instead of an inheritance. The inheritance is not an option if you want to define two classes. The relationship between two...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.