현재 다중 상속을 학습하고 이전 조상 변수 및 함수를 상속하는 함수를 만드는 동안 문제가 발생했습니다. 문제는 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
정확한 오류는 무엇인가요? – Aesthete
아, 죄송합니다. 방금 그것을 포함 시켰습니다! – SexyBeastFarEast