Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

friend class

Status
Not open for further replies.

bterribi

Programmer
Sep 27, 2006
8
0
0
US
/*
Dev-C++ 4.9.9.2

Will someone be so kind as to explain why I get
these errors at compile time:

line 30: variable or field 'opendoor' declared void
line 30: 'int person::eek:pendoor' is not a static member of 'class person'
line 30: 'house' was not declared in this scope
line 30: 'h' was not declared in this scope
line 51: 'class person' has no member named 'opendoor'
*/

#include <iostream>
using namespace std;
class door
{
public:
door(){lock=true;}
void setlock(bool l){lock=l;}
private:
bool lock;
};
class person
{
public:
person(){}
void opendoor(house * h);
};
void person::eek:pendoor(house * h)
{
bool l=false;
h->getd()->setlock(l);
}
class house
{
friend class person;

public:
house(){p=new person; d=new door;}
door* getd(){return d;}
person* getp(){return p;}
private:
person * p;
door * d;
};
int main()
{
house * h = NULL;
h = new house;
h->getp()->opendoor(h);
return 0;
}
 
Use [ignore]
Code:
[/ignore] tags when posting code.

Is that all in one file? (.h or .cpp?)

In your person class you use a house* pointer before defining the house class.

Why is person declared as a friend of house? It isn't accessing any of person's private functions or variables.
 
>Is that all in one file? (.h or .cpp?)

one cpp file

>In your person class you use a house* pointer before defining the house class.

I've tried a forward declaration of house before person and that reduced my errors to the following:

In member function `void person::eek:pendoor(house*)':
invalid use of undefined type `struct house'
forward declaration of `struct house'

>Why is person declared as a friend of house?

Ideally I would like person to be able to access my house's public member functions.



 
See void person::eek:pendoor(house * h) body: in this point members of class house are unknown, but you refer to (unknown!) getd() member.

Place this member function body after class house declaration, or better place all declarations to .h file then include it in implementation .cpp file.

Place
Code:
class house;
predeclaration before class person declaration for void person::eek:pendoor(house * h) member function parameter. No need to know person members for (person* h) only.
 
Thanks! I modified my code based on your suggestions and it now compiles :)

Code:
#include <iostream>
using namespace std;
class door
{
  public:
         door(){lock=true;}
         void setlock(bool l);
  private:    
         bool lock;
};
void door::setlock(bool l)
{
 lock=l;
 if(lock==false){cout<<"door is unlocked"<<endl;}   
}
class house;
class person
{
  public:
         person(){}
         void opendoor(house * h);    
};
class house
{
  friend class person;
  
  public:
         house(){p=new person; d=new door;}
         door* getd(){return d;}
         person* getp(){return p;}
  private:    
         person * p;
         door * d;
};
void person::opendoor(house * h)
{
 bool l=false;
 h->getd()->setlock(l);    
}
int main()
{
 house * h = NULL;
 h = new house;
 h->getp()->opendoor(h);
 system("pause");
 return 0;   
}
 
bterribi said:
Ideally I would like person to be able to access my house's public member functions.
Anyone in the universe can access a classes public data. The only thing you ever need friend for is if you need to access the private or protected data. But 99.9% of the time, if you find that you need to use the friend keyword, there's probably a better way to re-organize your code so you don't need to use it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top