2012-08-23 2 views
0

현재 다중 상속을 학습하고 이전 조상 변수 및 함수를 상속하는 함수를 만드는 동안 문제가 발생했습니다. 문제는 showRoomServiceMeal() 함수에서 발생합니다. 여러 함수를 상속합니다. 컴파일 할 때 해당 상속 된 변수는 보호되고 상속 된 함수는 개체없이 사용되므로 상속 될 수 없다는 오류가 발생합니다.보호 된 함수 및 공용 변수 상속에 대한 다중 상속 컴파일 오류 C++

나는 보호 된 평가자가 해당 변수를 자식에 의해 사용되도록 허용하고 해당 함수와 보호 된 변수에 액세스 할 수 있다고 생각했다. 범위 해결 연산자 (: :)가 사용됩니까? 아무도 왜 이러한 오류가 발생하는지 설명 할 수 있습니까? g을 사용하여

#include<iostream> 
#include<string> 
using namespace std; 

class RestaurantMeal 
{ 
protected: 
    string entree; 
    int price; 
public: 
    RestaurantMeal(string , int); 
    void showRestaurantMeal(); 
}; 

RestaurantMeal::RestaurantMeal(string meal, int pr) 
{ 
    entree = meal; 
    price = pr; 
} 

void RestaurantMeal::showRestaurantMeal() 
{ 
    cout<<entree<<" $"<<price<<endl; 
} 

class HotelService 
{ 
protected: 
    string service; 
    double serviceFee; 
    int roomNumber; 
public: 
    HotelService(string, double, int); 
    void showHotelService(); 
}; 

HotelService::HotelService(string serv, double fee, int rm) 
{ 
    service = serv; 
    serviceFee = fee; 
    roomNumber = rm; 
} 

void HotelService::showHotelService() 
{ 
    cout<<service<<" service fee $"<<serviceFee<< 
    " to room #"<<roomNumber<<endl; 
} 

class RoomServiceMeal : public RestaurantMeal, public HotelService 
{ 
public: 
    RoomServiceMeal(string , double , int); 
    void showRoomServiceMeal(); 
}; 

RoomServiceMeal::RoomServiceMeal(string entree, double price, int roomNum) : 
RestaurantMeal(entree, price), HotelService("room service", 4.00, roomNum) 
{ 
} 

void showRoomServiceMeal() 
{ 
    double total = RestaurantMeal::price + HotelService::serviceFee; 
    RestaurantMeal::showRestaurantMeal(); 
    HotelService::showHotelService(); 
    cout<<"Total is $"<<total<<endl; 
} 


int main() 
{ 
    RoomServiceMeal rs("steak dinner",199.99, 1202); 
    cout<<"Room service ordering now:"<<endl; 
    rs.showRoomServiceMeal(); 
    return 0; 
} 

++ 내가이 오류를 얻을 :

RoomService.cpp: In function ‘void showRoomServiceMeal()’: 
RoomService.cpp:18: error: ‘int RestaurantMeal::price’ is protected 
RoomService.cpp:73: error: within this context 
RoomService.cpp:18: error: invalid use of non-static data member ‘RestaurantMeal::price’ 
RoomService.cpp:73: error: from this location 
RoomService.cpp:39: error: ‘double HotelService::serviceFee’ is protected 
RoomService.cpp:73: error: within this context 
RoomService.cpp:39: error: invalid use of non-static data member ‘HotelService::serviceFee’ 
RoomService.cpp:73: error: from this location 
RoomService.cpp:74: error: cannot call member function ‘void RestaurantMeal::showRestaurantMeal()’ without object 
RoomService.cpp:75: error: cannot call member function ‘void HotelService::showHotelService()’ without object 
+0

정확한 오류는 무엇인가요? – Aesthete

+0

아, 죄송합니다. 방금 그것을 포함 시켰습니다! – SexyBeastFarEast

답변

1

당신 돈 (컴파일러를 만들고 그 함수는 정적 클래스가 아닌 관련 생각)

void showRoomServiceMeal() 
{ 
    ... 
} 
'은 t는 RoomServiceMeal 클래스의 일부로서 기능을 정의 showRoomServiceMeal 0

변경 또한

void RoomServiceMeal::showRoomServiceMeal() 
{ 
    ... 
} 

하려면 showRoomServiceMeal 방법 당신은 부모 클래스의 멤버에 액세스 할 때 클래스 접두사를 사용할 필요가 없습니다. 예를 들어 RestaurantMeal::price price을 사용해도됩니다. 이것은 변수와 함수가 각 클래스에서 고유하기 때문입니다.

+0

thanks mate. 하지만 고유 한 경우에도 모호성을 제거하기 위해 클래스 접두어를 사용하는 것이 더 좋지 않거나 단지 코딩 스타일입니까? – SexyBeastFarEast

+0

@SexyBeastFarEast 더 많은 코딩 스타일과 일반적인 경우에 게으름이 있습니다. :) –

0

을 당신은 RoomServiceMeal :: showRoomServiceMeal 전에()를 잊어

+0

ahh cheer mate! 나는 우리가 그것을 놓친다라고 생각한다. .. – SexyBeastFarEast