Whats making me scratch my head is that how does the class Monster know about the class Alien without me including the header file .Is this normal for the vs to know about where the friend class Alien is without including headers?
I am running this code under vs 2010
friendclass Alien; tells the compiler that a class named Alien is a friend of the Monster class. At this point it doesn't need to know anything more about the Alien class.
I guess when you declare a friend class ... then during compilation you compiler should be able to know where the friend class or function is located or declared
If you compiler is able to compile the code without you providing the headers of where your friend class is defined then that sounds like an error to me ..... I would also like to here about that from a more exprienced programmer ...
The error you got from you recent post is what should happen if you don't provide the headers or a prototype
I think this is a bit similar to when you define a member function you have to first declare it in the class definition. So it's just consistent with that. If the class has not been defined yet, or the class does not contain the function then you can't refer to the function.
so we can define a whole class to be a friend without including a header file
but not any of its perticular function whether its public or private ,right?
Pay attention to the difference between a declaration and definition.
A declaration tells the compiler that there is something.
1 2 3
// declarations:
class A; // there is a class named A
void foo(int); // there is a function named foo that takes an int as argument
A declaration tells the compiler what something is. (Note that a definition is also a declaration)
1 2 3 4 5 6 7 8 9 10 11 12
// definitions:
class A
{
public:
void doSomething();
private:
int b;
};
void foo(int value)
{
std::cout << value << std::endl;
}
vxk wrote:
so we can definedeclare a whole class to be a friend without including a header file
but not any of its perticular function whether its public or private ,right?
Yes that's right. To be able to declare a member function as friend you need the class definition.
1. A class declared at class scope of another class declares a nested class.
1 2 3 4
class Monster
{
class Alien; // this declares class Monster::Alien
...
This is an error:
1 2 3 4 5 6 7
class Alien { public: void poison() ; } ;
class Monster
{
class Alien; // this declares class Monster::Alien
friendvoid Alien::poison(); // *** error: incomplete type 'Monster::Alien' (declared, but not defined)
};
2. It is therefore evident that a friend declaration does not declare the type.
1 2 3 4 5
class A
{
friendclass B ; // this is a friend declaration; it is not a declaration of class B
B* pointer = nullptr ; // *** error: unknown type name 'B'. 'B' does not name a type
};