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;
} 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
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>
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 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" <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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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'...
|
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:
|
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...
|
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...
|
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);
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
| |