R
rory
Hi everyone. I'm trying to declare a function as a friend in one of my
classes. Correct me if I'm wrong but if I declare a friendly function
that means that I can access member of my class in that function? This
had been working for me with Borland C++ but once I changed to g++ I
get errors. Here's a brief example, I want to be able to access my
classes members from my friend function test():
#include <iostream>
using namespace std;
class myfirstclass
{
private:
public:
int number;
void greeting();
friend void test();
};
void test()
{
cout << myfirstclass->number;
}
void myfirstclass::greeting()
{
for(int i = 0; i<= number;i++)
{
cout<<number;
}
}
int main ()
{
myfirstclass myfirstobject;
myfirstobject.number =3;
myfirstobject.greeting();
return 0;
}
Any help is much appreciated,
Rory.
classes. Correct me if I'm wrong but if I declare a friendly function
that means that I can access member of my class in that function? This
had been working for me with Borland C++ but once I changed to g++ I
get errors. Here's a brief example, I want to be able to access my
classes members from my friend function test():
#include <iostream>
using namespace std;
class myfirstclass
{
private:
public:
int number;
void greeting();
friend void test();
};
void test()
{
cout << myfirstclass->number;
}
void myfirstclass::greeting()
{
for(int i = 0; i<= number;i++)
{
cout<<number;
}
}
int main ()
{
myfirstclass myfirstobject;
myfirstobject.number =3;
myfirstobject.greeting();
return 0;
}
Any help is much appreciated,
Rory.