Well the first compiler error is an indication that you are using the symbol House without first declaring it. Nowhere above that line is there anything which tells the compiler what "House" means - you leave that until later in the file. Therefore, the compiler does not know it's a type, and tries to interpret it as an object name.
Typically, what you do when writing C++ code is to put your class definition into a header file, and then include that header file at the top of every source file that uses it. So, in this case, you might have:
#include<iostream>
#include<string>
usingnamespace std; // Not a good idea to do this in a header
class House{
private:
string *nameowner;
int *numroom;
int *Rentingfee;
Room *room;
friendclass Town;
public:
House(){
setnameowner();
setnumroom();
Rentingfee=newint(0);}
~House(){
cout<<"bye House"<<endl;
delete []room;
}
void setnameowner(){
string a;
cout<<"enter the owner name of this house:";
cin>>a;
nameowner=new string(a);}
const string *getnameowner() const{
return nameowner;}
void setnumroom(){
int b;
cout<<"Enter the rooms number of this house:";
cin>>b;
numroom=newint(b);
setRoom(b);
}
constint *getnumroom()const{
return numroom;}
void setRoom(int n){
room=new Room[n];}
void setRentingFee(int index){
int m;
cout<<"Enter the monthly Renting fee of this house:";
cin>>m;
Rentingfee=newint(m);
}
constint *getRentingFee()const{
return Rentingfee;
}
void DisplayHouse(){
cout<<"the house name is :"<<getnameowner()<<endl;
cout<<"the Renting fee is : "<<getRentingFee()<<endl;
cout<<"the number of rooms in this house is :"<<getnumroom()<<endl;
cout<<"and the rooms details as follows:"<<endl;
for(int i=0;i<*numroom;i++)
room[i].DisplayRoom();}
};
#include<iostream>
#include<string>
#include "House.h" // This takes all the code from House.h and includes it here
usingnamespace std;
class Town{
private:
House *homes;
bool *Rented;
string *NameTown;
staticint Num;
public:
Town(string n ){
setnametown(n);
Rented= newbool [5];
for ( int i=0;i<5;i++){
Rented [i]=0;
sethomes();
Num++;} // end constructor
~Town();
cout<<"bye Town"<<endl;
delete []homes ;
homes=0;} // end destructor
void setnametown( string n ){
cout<<"Enter the name of this Town ::";
cin>>n;
NameTown=new string(n);}
const string *getNameTown() const{
return NameTown;}
int getNum(){
return Num;}
void sethomes(){
homes=new House[5];}
void Addhouse(House a ,int index){
homes[index]=a;
setRented(index);
if( Rented[index]=1)
homes[index]. setRentingFee(index);
else
cout<<"No Rening fee"<<endl;}
void setRented(int index){
IsRented( index);}
void IsRented(int index){
bool a;
cout<<"Is this house with index"<<index<<"rented?? "<<endl;
cout<<"if yes enter 1 else enter 0"<<endl;
cin>>a;
if(a){
Rented[index]=1;
cout<<"this house is rented"<<endl;}
else
cout<<" this house is not rented !"<<endl;
}
void AvgRentFees(){
int sum =0;
for ( int i=0;i<5;i++)
sum+=*(homes[i].Rentingfee);
cout<<"the Average Renting fee is "<<sum/5<<endl;}
void Cheapest(){
int mini=*(homes[0].Rentingfee);
string name=*homes[0].nameowner;
for( int i=0;i<5;i++)
if(mini>*(homes[i].Rentingfee)){
mini=*(homes[i].Rentingfee);
name=*(homes[i].nameowner);}
cout<<"the least renting fee is "<<mini<<"and the name of its owner"<<name<<endl;}
void HomeArea(int index){
int sum=0;
int a=*( homes[index].numroom);
for(int i=0;i<a;i++)
sum+=homes[index].room[i].Area;
cout<<"The Total Area of house index"<<index<<"IS;;"<<sum<<endl;}
void DisplayTowm(){
cout<<"the name of this Town is "<<&getNameTown<<endl;
cout<<"the number of objects in this Town is :"<<getNum()<<endl;
cout<<"this Town includes 5 homes and their info as following :";
for ( int i=0;i<5;i++){
cout<<"info of house "<<i<<" : :";
Addhouse(homes[i],i);
homes[i].DisplayHouse();}
};
class Room{
private:
int Area;
string roomname;
friendclass House;
friendclass Town;
public:
Room( int a, string n){
setArea(a );
setroomname( n);}
~Room(){
cout<<"bye room"<<endl;}
void setArea(int a){
cout<<"Enter the area of this room ";
cin>>a;
Area=a;}
int getArea(){
return Area;}
void setroomname(string n){
cout<<"Enter the name of this room ";
cin>>n;
roomname=n;}
string getnameroom(){
return roomname;}
void DisplayRoom(){
cout<<"name of room :"<<&getnameroom<<endl;
cout<<"room Area:"<<&getArea<<endl;
}};
void main()
{
Town d("bayan");
}
That way, you've defined what "House" means before trying to use it.
Typically, you wouldn't keep the definitions of the House methods in the header file; you'd move them to a source file, so that the header would contain only method declarations:
so ok I did this now and I put 7 files
3 files .h
3 files .cpp
main.cpp
but it gives me::
Error 4 error LNK1120: 2 unresolved externals C:\Users\MICROSOFT\Documents\Visual Studio 2010\Projects\New folder\newclasses\Debug\newclasses.exe
Error 3 error LNK2001: unresolved external symbol "private: static int Town::Num" (?Num@Town@@0HA) C:\Users\MICROSOFT\Documents\Visual Studio 2010\Projects\New folder\newclasses\newclasses\Town.obj
Error 2 error LNK2001: unresolved external symbol "public: __thiscall Town::~Town(void)" (??1Town@@QAE@XZ) C:\Users\MICROSOFT\Documents\Visual Studio 2010\Projects\New folder\newclasses\newclasses\Town.obj
Error 1 error LNK2019: unresolved external symbol "public: __thiscall Town::~Town(void)" (??1Town@@QAE@XZ) referenced in function _main C:\Users\MICROSOFT\Documents\Visual Studio 2010\Projects\New folder\newclasses\newclasses\main.obj
my code is true 100%
and I wrote it by the perfect way and my C++ program is perfect no prob in system or any thing but when debug it gives me error like this:
Error 1 error LNK1123: failure during conversion to COFF: file invalid or corrupt C:\Users\MICROSOFT\Documents\Visual Studio 2010\Projects\New folder\newclasses\newclasses\LINK