【问题标题】:Derived class does not have access to inherited function?派生类不能访问继承的函数?
【发布时间】:2011-05-12 14:34:34
【问题描述】:

我正在创建一个涉及继承的非常简单的程序。我将一个函数放入父类的“受保护”区域,现在我无法从子类访问。这是我的代码:

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() 函数不可访问。

这里有什么帮助吗?谢谢!!

【问题讨论】:

  • printGame 在 Game 中是私有的。前面不应该有“public:”吗?
  • 请停止使用动词“throw”来表示编译时错误。 Throw 是一个 C++ 关键字,用于引发异常,仅在运行时发生。
  • 另外,受保护的继承是怎么回事?甚至 Bjarne Stroustrup 在“C++ 编程语言”中也提到它是一个他永远无法提出用例的特性。

标签: c++ inheritance derived


【解决方案1】:

您的&lt;&lt; 运算符是用const Game &amp; 对象调用的,这意味着该函数只能调用Gameconst 成员函数

const 添加到getPriceprintGame

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

您还必须公开printGame

【讨论】:

    【解决方案2】:

    getPrice() 方法是 Product 的成员,而不是 Item。所以从 Item 派生 Toy 不会让它访问 getPrice()。它必须派生自 Product(或其子类之一)。

    【讨论】:

      【解决方案3】:

      Game 的成员可以访问getPrice,但您的operator&lt;&lt; 不是成员,也不应该是。

      改为使用朋友声明来授予operator&lt;&lt;(ostream&amp;, const Game&amp;) 访问权限。

      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()
      
         << "." ;
      }
      

      【讨论】:

        【解决方案4】:

        getPrice() 方法是 Product 类的成员,但您从 Item 派生。

        另外 - 如果你会这样称呼它,那么我相信你需要一个 const 函数来调用 Product 类

        【讨论】:

          【解决方案5】:

          我对此进行了尝试,但必须做出一些假设才能使其发挥作用。 如所写,代码应该可以工作

          #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;
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-03-26
            • 2012-12-19
            相关资源
            最近更新 更多