2

継承を含む非常に単純なプログラムを作成しています。親クラスの「保護された」領域に関数を配置しましたが、子クラスからアクセスできなくなりました。これが私のコードです:

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() 関数にアクセスできないことを知らせる追加のエラーが表示されます。

ここで何か助けはありますか?ありがとう!!

4

5 に答える 5

4

演算子<<はオブジェクトで呼び出されます。これは、関数がメンバー関数const Game &のみを呼び出すことができることを意味しますconstGame

constgetPriceに追加printGame:

double getPrice() const {return Price;}
double printGame() const {return getPrice();}

また、公開する必要がありますprintGame

于 2011-05-12T14:38:47.937 に答える
0

getPrice() メソッドは、Item ではなく Product のメンバーです。したがって、Toy を Item から派生させても、getPrice() へのアクセスは許可されません。Product (またはそのサブクラスの 1 つ) から派生する必要があります。

于 2011-05-12T14:36:05.497 に答える
0

私はこれを突き刺しましたが、それを機能させるにはいくつかの仮定を立てる必要がありました. 書かれているように、コードは機能するはずです

#include <iostream>

using std::ostream;

// I added this to make sure it looks close to your stated problem
class Item
{
};

class Product : protected Item 
{
private:    
    double Price;
protected:   
    // getter needs to be const, for the ostream operator to work
    double getPrice() const
    {
        return Price;
    }
};

// I changed this to derive from Product, otherwise, there's no inheritance tree.
// Is this what you intended to do?
class Toy : protected Product
{
};

class Game : protected Toy
{  
    // if you don't specify an access modifier, C++ defaults to private
public:
    // getter needs to be const, for the ostream operator to work
    double printGame() const
    {
        return getPrice();
    }
};

ostream& operator << (ostream& Output, const Game &printedGame)
{
    return Output  << "The game price is: " << printedGame.printGame() << "." ;
}

int _tmain(int argc, _TCHAR* argv[])
{
    return 0;
}
于 2011-05-12T15:19:02.057 に答える
0

getPriceのメンバーは にアクセスできGameますが、あなたoperator<<はメンバーではありませんし、そうすべきでもありません。

代わりに、フレンド宣言を使用してoperator<<(ostream&, const Game&)アクセスを許可してください。

class Product : protected Item 
{

private:

  double Price;

protected:

  double getPrice() const {return Price;}
};

class Toy : protected Product
{
};

class Game : protected Toy
{
  friend ostream& operator<<(ostream&, const Game&);
};

ostream& operator << (ostream& Output, const Game &printedGame)
{
  return Output 

   << "The game price is: "

   //This is the problem line

   << printedGame.getPrice()

   << "." ;
}
于 2011-05-12T14:38:53.963 に答える
0

getPrice() メソッドは Product クラスのメンバーですが、Item から派生します。

また、そのように呼び出す場合は、 Product クラスで呼び出すには const 関数が必要だと思います

于 2011-05-12T14:40:25.000 に答える