継承を含む非常に単純なプログラムを作成しています。親クラスの「保護された」領域に関数を配置しましたが、子クラスからアクセスできなくなりました。これが私のコードです:
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() 関数にアクセスできないことを知らせる追加のエラーが表示されます。
ここで何か助けはありますか?ありがとう!!