2014-01-05 58 views
0

我根據這本書做了一個freind函數程序,並且我爲自己的程序做了一點我自己的代碼。我困惑,因爲我得到這個錯誤消息,「room_num」未聲明和智能識別標識符「room_num」未定義。我需要幫助理解爲什麼發生這種情況以及如何解決這個問題。這是我一直在努力的三個星期的代碼。不理解我收到的錯誤消息

#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; 
    } 
+2

如果您包含堆棧跟蹤或可能指示您的程序中遇到錯誤,那將會很有幫助。 – STLDeveloper

+0

除了發佈完整的錯誤消息,您還應該使用適當的和隨之而來的縮進。 – Nabla

+0

你應該學會準備[最小完整示例](http://sscce.org)。這是一種比特定編譯器錯誤的知識更有價值的技能。 – Beta

回答

0

功能Transfer不是HotelRoom的方法,還是你想在它訪問room_num,彷彿它是。您需要指定哪個room_num其中的HotelRoom您的意思。可能你的意思是return room_r1.room_num而不是return room_num

此外,在您的Transfer功能中,您從不使用參數transroom,而是使用room_r1transroom_num。這可能不是你想要的。

最後你還沒有實現HotelRoom的構造函數和DisplayRoom。你應該創建一個存根,只要你沒有正確地實現這些方法,它們什麼也不做或者打印警告,所以你至少可以編譯和鏈接代碼。

0

既然你是初學者,我會堅持使用成員函數和類的私有變量,直到你變得更好。

至於錯誤消息,我的猜測是你使用room_num的函數內部沒有訪問HotelRoom類的私有部分。注意我說過猜測,那是因爲你應該在這裏複製並粘貼輸出窗口上的文本,這樣我們就可以看到究竟發生了什麼。

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

相關問題