Derived class does not have access to inherited function?
I'm creating a very simple program that involves inheritance. I put a function into the "protected" area of the parent class, and now I don't have access from the child class. Here is my code:
class Product : protected Item
{
private:
double Price;
protected:
double getPrice(){return Price;}
//other code not connected
};
Later on, I derive:
class Toy : protected Item
{
// class Toy code that does not mention getPrice() at all
};
And after that, I derive another class in which I actually try to use the getPrice() function.
In the new class's header file:
class Game : protected Toy
{
double printGame(){return getPrice();}
};
This line does not give me an error.
But in the file game.cpp:
ostream& operator << (ostream& Output, const Game &printedGame)
{
return Output
<< "The game price is: "
//This is the problem line
<< printedGame.printGame开发者_高级运维()
<< "." ;
}
The word "printedGame" returns me "Error: the object has type qualifiers that are not compatible with the member function"
When I tried going directly (which I tried before, as such:)
printedGame.getPrice()
I get that error, and an additional one informing me that the getPrice() function is not accessible.
Any help here? Thanks!!
Your <<
operator is called with a const Game &
object which means that the function may only call const
member functions of Game
Add const
to getPrice
and printGame
:
double getPrice() const {return Price;}
double printGame() const {return getPrice();}
You'll also have to make printGame
public.
The getPrice() method is a member of Product, not Item. So deriving Toy from Item will not give it access to getPrice(). It would have to derive from Product (or one of its sub-classes.)
getPrice
is accessible to members of Game
, but your operator<<
is not a member, nor should it be.
Instead, use a friend declaration to give operator<<(ostream&, const Game&)
access.
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()
<< "." ;
}
getPrice() method is a member of Product class but you derive from Item.
Also - if you would call it like that then I believe you need a const function to call in Product class
I took a stab at this, but had to make some assumptions to make it work. As written, the code should work
#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;
}
精彩评论