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



 DEVELOP > c-Plus-Plus > Can't access a static private data member from a friend function?

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "JustSomeGuy"
Date: 01 Dec 2004 07:53:08 PM
Object: 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;
}
.

User: "Jonathan Turkanis"

Title: Re: Can't access a static private data member from a friend function? 01 Dec 2004 08:18:43 PM
"JustSomeGuy" <nope@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
.

User: "Sumit Rajan"

Title: Re: Can't access a static private data member from a friend function? 01 Dec 2004 09:01:03 PM
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 <sumit.rajan@gmail.com>
.

User: "Daniel Mitchell"

Title: Re: Can't access a static private data member from a friend function? 05 Dec 2004 12:59:59 AM
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; }
.
User: "Daniel Mitchell"

Title: Re: Can't access a static private data member from a friend function? 05 Dec 2004 01:06:59 AM
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.
.
User: "JustSomeGuy"

Title: Re: Can't access a static private data member from a friend function? 05 Dec 2004 11:51:20 AM
"Daniel Mitchell" <danmitchell@mail.utexas.edu> wrote in message
news:couc38$rlj$1@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.
.




  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER