Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Can't access a static private data member from a friend function?

Reply
Thread Tools

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

 
 
JustSomeGuy
Guest
Posts: n/a
 
      12-02-2004
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;
}


 
Reply With Quote
 
 
 
 
Jonathan Turkanis
Guest
Posts: n/a
 
      12-02-2004

"JustSomeGuy" <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
 
Sumit Rajan
Guest
Posts: n/a
 
      12-02-2004
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 <(E-Mail Removed)>
 
Reply With Quote
 
Daniel Mitchell
Guest
Posts: n/a
 
      12-05-2004
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; }


 
Reply With Quote
 
Daniel Mitchell
Guest
Posts: n/a
 
      12-05-2004
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.


 
Reply With Quote
 
JustSomeGuy
Guest
Posts: n/a
 
      12-05-2004

"Daniel Mitchell" <(E-Mail Removed)> wrote in message
news:couc38$rlj$(E-Mail Removed)...
> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can a static member function access non-static member? dolphin C++ 3 12-05-2007 12:39 PM
how to access a private member function without using friend and virtual rajasekaran.psg@gmail.com C++ 2 01-05-2007 10:07 AM
Private data access using friend Jaspreet C++ 1 03-10-2006 02:34 PM
private static member access by a friend class Anjo Gasa C++ 2 02-27-2006 05:17 PM
private friend member function - should this compile or not? Andre Eisenbach C++ 6 09-11-2005 03:28 AM



Advertisments