我正在创建一个涉及继承的非常简单的程序。我将一个函数放入父类的“受保护”区域,现在我无法从子类访问。这是我的代码:
class Product : protected Item
{
private:
double Price;
protected:
double getPrice(){return Price;}
//other code not connected
};
后来,我得出:
class Toy : protected Item
{
// class Toy code that does not mention getPrice() at all
};
之后,我派生了另一个类,我在其中实际尝试使用 getPrice() 函数。
在新类的头文件中:
class Game : protected Toy
{
double printGame(){return getPrice();}
};
这条线不会给我一个错误。
但在文件game.cpp中:
ostream& operator << (ostream& Output, const Game &printedGame)
{
return Output
<< "The game price is: "
//This is the problem line
<< printedGame.printGame()
<< "." ;
}
“printedGame”一词返回“错误:对象具有与成员函数不兼容的类型限定符”
当我尝试直接进行时(我之前尝试过,因此:)
printedGame.getPrice()
我收到了那个错误,还有一个通知我 getPrice() 函数不可访问。
这里有什么帮助吗?谢谢!!