Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot cast "derived" to its private base class "base" [duplicate]

I'm encountering an error when trying to create an object which inherits from a class which inherits from a class which defines pure virtual functions. I'm not sure what is wrong. I know I need to override the pure virtual functions in my derived class but it's not working. I only want to override the function in my ProduceItem class and not my Celery class because I want the Celery class to inherit the overridden methods from ProduceItem.

In Main:

    GroceryItem *cel = new Celery(1.5); //Cannot cast 'Celery' to its private base class GroceryItem


    class GroceryItem
    {
    public:
        virtual double GetPrice() = 0;
        virtual double GetWeight() = 0;
        virtual std::string GetDescription() = 0;

    protected:
        double price;
        double weight;
        std::string description;

    };

ProduceItem header file:

#include "GroceryItem.h"

class ProduceItem : public GroceryItem
{
public:
    ProduceItem(double costPerPound);
    double GetCost();
    double GetWeight();
    double GetPrice();
    std::string GetDescription();
protected:
    double costPerPound;
};

ProduceItem.cpp file:

#include <stdio.h>
#include "ProduceItem.h"
ProduceItem::ProduceItem(double costPerPound)
{
    price = costPerPound * weight;
}

double ProduceItem::GetCost()
{
    return costPerPound * weight;
}

double ProduceItem::GetWeight()
{
    return weight;
}

double ProduceItem::GetPrice()
{
    return price;
}

std::string ProduceItem::GetDescription()
{
    return description;
}

Celery header file:

#ifndef Lab16_Celery_h
#define Lab16_Celery_h
#include "ProduceItem.h"

class Celery : ProduceItem
{
public:
    Celery(double weight);
    double GetWeight();
    double GetPrice();
    std::string GetDescription();

};

#endif

Celery.cpp file:

#include <stdio.h>
#include "Celery.h"

Celery::Celery(double weight) : ProduceItem(0.79)
{
    ProduceItem::weight = weight;
    description = std::string("Celery");
}
like image 415
user2771729 Avatar asked Mar 04 '15 10:03

user2771729


People also ask

Can derived class access private members of base?

Private members of the base class cannot be used by the derived class unless friend declarations within the base class explicitly grant access to them.

Are private class members inherited to the derived class?

The derived class doesn't "inherit" the private members of the base class in any way - it can't access them, so it doesn't "inherit" them.

Can a base class be derived?

The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. A derived class can have only one direct base class. However, inheritance is transitive.

Can derived class access public members of base class?

You can derive classes using any of the three access specifiers: In a public base class, public and protected members of the base class remain public and protected members of the derived class. In a protected base class, public and protected members of the base class are protected members of the derived class.


2 Answers

You are using private inheritance here: class Celery : ProduceItem. The default inheritance level for classes is private.

Change it to class Celery : public ProduceItem

Note that when you delete that pointer, you will leak memory because you don't have virtual destructors. Just add definitions like this to your classes:

virtual ~GroceryItem() {}
like image 71
TartanLlama Avatar answered Nov 03 '22 14:11

TartanLlama


Inheritance of classes is private by default. Usually, and in your case, you want public inheritance. So:

class Celery : public ProduceItem

And similar on class ProduceItem.

Note that for structs, inheritance is public by default (you can say struct Foo : private Bar if you want).

like image 11
John Zwinck Avatar answered Nov 03 '22 13:11

John Zwinck