473,396 Members | 1,748 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,396 software developers and data experts.

Can't access a static private data member from a friend function?

I have a class with a private data member and I want to access it
from a friend function, but Visual Studio 2003 .NET won't let me.

class MyClass
{
private:
static int level;
public:
friend ostream & operator<<(ostream & x, const MyClass & c);
}

int MyClass::level = 0;

ostream & operator<<(ostream & x, const MyClass & c)
{
O << c.level;
}
Jul 22 '05 #1
5 1941

"JustSomeGuy" <no**@nottelling.com> wrote in message
news:8Aurd.408074$%k.113318@pd7tw2no...
I have a class with a private data member and I want to access it
from a friend function, but Visual Studio 2003 .NET won't let me. class MyClass
{
private:
static int level;
public:
friend ostream & operator<<(ostream & x, const MyClass & c);
}

int MyClass::level = 0;

ostream & operator<<(ostream & x, const MyClass & c)
{
O << c.level;
}


Post some code that has some hope of compiling. Among other things, you've
omitted the semicolon at the end of the definition of MyClass, you've used an
undeclared variable O in the implementation of operator<<, and the function
doesn't return a value.

Jonathan
Jul 22 '05 #2
JustSomeGuy wrote:
I have a class with a private data member and I want to access it
from a friend function, but Visual Studio 2003 .NET won't let me.

#include <iostream>

using namespace std;

class MyClass
{
private:
static int level;
public:
friend ostream & operator<<(ostream & x, const MyClass & c);
}
missing ;
int MyClass::level = 0;

ostream & operator<<(ostream & x, const MyClass & c)
{
O << c.level;
x << c.level; //or "x << MyClass::level;"
return x; // undefined behaviour if you forget to return x
}


Regards,
Sumit.
--
Sumit Rajan <su*********@gmail.com>
Jul 22 '05 #3
JustSomeGuy wrote:
I have a class with a private data member and I want to access it
from a friend function, but Visual Studio 2003 .NET won't let me.

class MyClass
{
private:
static int level;
public:
friend ostream & operator<<(ostream & x, const MyClass & c);
}

int MyClass::level = 0;

ostream & operator<<(ostream & x, const MyClass & c)
{
O << c.level;
}


ostream& operator<<( ostream& x, const MyClass& c )
{ return x << c::level; }
Jul 22 '05 #4
Daniel Mitchell wrote:
JustSomeGuy wrote:
I have a class with a private data member and I want to access it
from a friend function, but Visual Studio 2003 .NET won't let me.

class MyClass
{
private:
static int level;
public:
friend ostream & operator<<(ostream & x, const MyClass & c);
}

int MyClass::level = 0;

ostream & operator<<(ostream & x, const MyClass & c)
{
O << c.level;
}


ostream& operator<<( ostream& x, const MyClass& c )
{ return x << c::level; }


I'm retarded. Sorry for wasting everyones time.
Jul 22 '05 #5

"Daniel Mitchell" <da*********@mail.utexas.edu> wrote in message
news:co**********@geraldo.cc.utexas.edu...
Daniel Mitchell wrote:
JustSomeGuy wrote:
I have a class with a private data member and I want to access it
from a friend function, but Visual Studio 2003 .NET won't let me.

class MyClass
{
private:
static int level;
public:
friend ostream & operator<<(ostream & x, const MyClass & c);
}

int MyClass::level = 0;

ostream & operator<<(ostream & x, const MyClass & c)
{
O << c.level;
}


ostream& operator<<( ostream& x, const MyClass& c )
{ return x << c::level; }


I'm retarded. Sorry for wasting everyones time.

I doubt that... Thank you.
Jul 22 '05 #6

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

Similar topics

3
by: Rajesh Garg | last post by:
Can we have private constructors and destructors? IF yes what is the use of such constructors or destructors.....in the sense where can these be implemented in a system................. I have...
12
by: Bryan Parkoff | last post by:
CMain Class is the base class that is initialized in main function. CA Class is the base class that is initialized in CMain::CMain(). CMain Class is always public while CA Class is always...
8
by: SJ | last post by:
Hi: I have a class which has a static member function. The function implements something common to all instances. How can the static member function know all of the (Get access to the instances'...
4
by: Eric A. Johnson | last post by:
Hi All, I have a class, ConsoleWindow, that is a member of another class, ConsoleLib, like so: class ConsoleLib { public: class ConsoleWindow { public:
6
by: blueblueblue2005 | last post by:
here is a friend function of Class Array, which has two private data member: int size, int *ptr // Array's public member function to return size int getSize() const { return size; } friend...
2
by: David Adams | last post by:
Hi, I am afraid I am asking a very basic question, or just something that is not possible. I have a WinForm app that contains a Form (Form A), and then 20-30 UserControls/WinForms that are...
2
by: freegnu | last post by:
how to declare a friend function that can access two class it will look like the following class A { private: int i; public: A(){} ~A(){} friend void call(A &a, B &b);
1
by: yancheng.cheok | last post by:
currently, i have a private function in cat named privateFun. i would like to have this function "private" to all except dog's action member function. by using the following approach, all the...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...

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.