2011-05-12 52 views
2

我正在創建一個涉及繼承的非常簡單的程序。我把一個函數放到父類的「protected」區域,現在我沒有從子類中訪問。這裏是我的代碼:派生類無權訪問繼承函數?

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()函數是無法訪問的。

這裏有什麼幫助嗎?謝謝!!

+1

printGame在遊戲中是私人的。在它之前不應該有「公開:」嗎? – 2011-05-12 14:40:15

+0

請停止使用動詞「throw」編譯時錯誤。 Throw是一個C++關鍵字,用於引發異常,這隻發生在運行時。 – 2011-05-12 14:55:29

+0

另外,受保護的繼承是什麼?即使Bjarne Stroustrup在「C++編程語言」中也提到了它作爲一個功能,他永遠不會想出一個用例。 – 2011-05-12 14:57:14

回答

0

getPrice()方法是Product的成員,而不是Item。所以從Item派生Toy不會給它getPrice()的訪問權限。它必須從產品到獲得(或它的子類之一。)

4

<<運營商是帶一個const Game &對象,這意味着該函數只能調用const成員函數的Game

添加constgetPriceprintGame

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

您還可以使公衆printGame

+1

'const'是這裏對應錯誤的問題。 – Puppy 2011-05-12 14:40:29

0

getPriceGame的會員可以訪問,但您的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() 

    << "." ; 
} 
0

getPrice()方法是Product類的成員,但是您從Item派生。

而且 - 如果你會說它是這樣的話,我相信你需要一個const函數在產品類

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