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

Discussion in 'C++' started by JustSomeGuy, Dec 2, 2004.

  1. JustSomeGuy

    JustSomeGuy Guest

    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;
    }
    JustSomeGuy, Dec 2, 2004
    #1
    1. Advertising

  2. "JustSomeGuy" <> 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
    Jonathan Turkanis, Dec 2, 2004
    #2
    1. Advertising

  3. JustSomeGuy

    Sumit Rajan Guest

    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, Dec 2, 2004
    #3
  4. 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; }
    Daniel Mitchell, Dec 5, 2004
    #4
  5. 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.
    Daniel Mitchell, Dec 5, 2004
    #5
  6. JustSomeGuy

    JustSomeGuy Guest

    "Daniel Mitchell" <> wrote in message
    news:couc38$rlj$...
    > 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.
    JustSomeGuy, Dec 5, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bryan Parkoff
    Replies:
    12
    Views:
    924
    Bryan Parkoff
    Apr 12, 2004
  2. Anjo Gasa
    Replies:
    2
    Views:
    430
    Rolf Magnus
    Feb 27, 2006
  3. mrstephengross
    Replies:
    3
    Views:
    464
  4. Replies:
    2
    Views:
    705
    Ondra Holub
    Jan 5, 2007
  5. dolphin
    Replies:
    3
    Views:
    1,328
    Pete Becker
    Dec 5, 2007
Loading...

Share This Page