不理解我得到的错误信息

时间:2014-01-05 03:08:57

标签: c++

我根据这本书做了一个freind函数程序,我做了一些我自己的程序代码。我很困惑,因为我得到这个错误消息,“room_num”未声明,智能感知标识符“room_num”是undefine。我需要帮助来理解为什么会发生这种情况以及如何解决它。这是我过去三周一直在做的代码。

 #include "stdafx.h"
 #include <iostream>
 #include <iomanip>


 using namespace std;

 class HotelRoom
{
friend int Transfer( HotelRoom&, int);
 private:
int room_num; 
int transroom_num;
int room_cap;
int occup_stat;
double daily_rt;


public:
HotelRoom(int room, int roomcap, int occup,  int transroom, double rate = 89.00);
~HotelRoom();
int Display_Number();  //Displays room number and add the method Display_Guest()
int Get_Capacity();
int Get_Status();
double Get_Rate();
int Change_Status(int);
double Change_Rate(double);
void Display_Guest();
};

 HotelRoom::~HotelRoom()
{
cout << endl<<endl;
cout << "Guest in room "<<room_num << " has checked out." <<endl;
}

 int HotelRoom::Display_Number()
{
return room_num;
}

 int HotelRoom::Get_Capacity()
{
return room_cap;
}

 int HotelRoom::Get_Status()
{

return occup_stat;
}



    int HotelRoom::Change_Status(int occup)
{
occup_stat = occup;

if (occup > room_cap)
{
    return -1;
}
else

return occup_stat;

   }

   double HotelRoom::Get_Rate()
  {
return daily_rt;
  }

   double HotelRoom::Change_Rate(double rate)
  {
daily_rt = rate;
    return daily_rt;
  }

   int Transfer(HotelRoom& room_r1, int transroom)

  {
//if guest transfers to different hotel room, room is vacant and transroom is now        occupied
room_r1.room_num = room_r1.transroom_num;



return room_num;

  }


   int main()
 {
cout<< setprecision(2)
    <<setiosflags(ios::fixed)
    <<setiosflags(ios::showpoint);

   int room = 0; 
   int roomcap = 4;
   int transroom;
   int occup;
   double rate = 89.00;


   cout<<"\nEnter the room number: "<<endl;
   cin>>room;

   cout<<"\nEnter the amount of guest to occupy this room: "<<endl;
   cin>>occup;


   cout<<"\nThe guest has decided to transfer rooms"<<endl;
   cout<<"\nEnter the room to transfer the guest to"<<endl;
   cin>>transroom;

   HotelRoom room1(room,roomcap, occup, transroom, rate ); //initialize the object

   if (room1.Change_Status(occup) == -1)
   {
cout<<"You have exceeded the room capacity"<<endl;
   }
   else
   {

    cout <<"\nThe room number is ";
    room1.Display_Number();
    cout<<"."<<endl;
    cout<<"\nThe name of the primary guest is ";
    room1.Display_Guest();
    cout <<"."<<endl;
    cout<<"\nThe number of guest in the room is "<<room1.Change_Status(occup)<<"."    <<endl;
   cout<<"\nThe daily rate for room "<<room<< " is "<<room1.Get_Rate()<<"."<<endl<<endl;



   cout<<"\nYou have tranferred the guest from room"<<room1.Display_Number()<<"to"     <<Transfer(room1,transroom)<<endl;
    }

     cout<<"\nRoom ";
     room1.Display_Number();
     cout<<" is vacant."<<endl;

     system("PAUSE");


return 0;
    }

3 个答案:

答案 0 :(得分:0)

函数Transfer不是HotelRoom的方法,您仍然试图访问其中的room_num,就像它一样。您需要指定您所指的room_numHotelRoom个实例return room_r1.room_num。可能你的意思是return room_num而不是Transfer

同样在transroom功能中,您永远不会使用参数transroom_num,而是使用room_r1中的DisplayRoom。这可能不是你想要的。

最后,您还没有实现HotelRoom的构造函数和{{1}}。您应该创建一个存根,只要您没有正确实现方法,它们什么也不做或打印警告,因此您至少可以编译和链接代码。

答案 1 :(得分:0)

由于你是初学者,我会坚持使用成员函数和类私有变量,直到你变得更好。

就错误消息而言,我的猜测是你在使用room_num的函数内部无法访问HotelRoom类的私有部分。注意我说是猜,那是因为你应该在输出窗口复制并粘贴文本,这样我们才能看到究竟发生了什么。

答案 2 :(得分:0)

首先,您必须确定room_num是类成员变量。

int Transfer(HotelRoom& room_r1, int transroom)
{
    room_r1.room_num = room_r1.transroom_num;


    //because room_num is not non class member variable, you have to write like below.
    return room_r1.room_num;
    //return room_num;
}

其次,您没有写定义HotelRoom::HotelRoom(int,int,int,int,double)HotelRoom::Display_Guest(void)。因此,您必须编写此构造函数和函数以避免错误LNK2019