473,757 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2450
Srini nandiraju wrote in news:b6da7d0d.0 405181949.1d79f 9f3
@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******@ureac h.com> wrote in message
news:b6******** *************** ***@posting.goo gle.com...
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******@ureac h.com> wrote in message
news:b6******** *************** ***@posting.goo gle.com...
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******@ureac h.com> wrote in message
news:b6******** *************** ***@posting.goo gle.com...
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
8594
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
6591
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 either has access only to static members of the class (ie. assuming no object of the class is in scope - neither by arguments recieved nor by local declarations). Any static member function like this: //accessing static member i static void...
7
9822
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 within the same program/assembly. The two classes are not 'related' (inherited). How can I do this. I cant see how the keyword Internal can be used for this purpose.
4
4319
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 there is a nice overview of the different types of classes (public, private, shared, friend, etc.) and how they differ on a broad level, I'd certainly like to read it. I'm at the point where I'm a 'decent hack' and .net but I'm missing some...
2
1713
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 want any other classes to be able to use this smaller class, as it just wouldn't make sense. I figured that I could either declare the smaller class inline in my larger class (not keen on doing that as it makes a mess!) or give the smaller class...
8
1484
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) IParser, one for XML, ne for Config, & one for binary. They need to get the Document & CC class and fill the data, in the
3
1735
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
2162
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
2721
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 public data members. Don't use friends, except to avoid or . Consider the following scenarios: Scenario 1:
1
2529
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 classes can be friends when variables can be modified in both classes. As far as we discussed in an earlier post, but I try to make this code much clearer. The C++ Compiler generates an error if you try member function to be friend because member...
0
9487
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9297
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10069
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9904
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9735
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8736
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7285
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3828
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3395
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.