首页 > 解决方案 > 无法使用继承创建参数化构造函数,“不是非静态数据成员或类的基类”

问题描述

当我尝试为我的蔬菜类创建参数化构造函数时,出现此错误:不是非静态数据成员或类的基类。错误发生在实现构造函数的 Vegetable.cpp 文件中。

杂货店.hpp

#include "Grocery.hpp"          
class Grocery{
public:

    Grocery();
    Grocery(std::string name, double price, double weight);


    bool operator==(const Grocery &rhs) const;      // Comparison operator overload
protected:
    
    std::string name_;
    int quantity_;
    double unit_price_;
    double unit_weight_;
    double total_price_;}; // end Grocery
    #include "Grocery.cpp"
    #endif

蔬菜.hpp

#ifndef Vegetable_
#define Vegetable_

#include "Grocery.hpp"
#include <string>

class Vegetable : public Grocery{
public:
Vegetable(std:: string,double price, double weight );
virtual void updateCost() override;
}

蔬菜.Cpp

#include <iostream>
#include <string>
#include "Vegetable.hpp"

Vegetable ::Vegetable(std :: string name, double price, double weight):name_(name), 
unit_price_(price), unit_weight_(weight){            
}

错误:

“name_”不是类“Vegetable”
的非静态数据成员或基类 “unit_price_”不是类“Vegetable”的非静态数据成员或基类
“unit_weight_”不是类“Vegetable”的非静态数据成员或基类"

为什么说 name_、unit_price_ 和 unit_weight_ 不是我的蔬菜类的成员?我不是继承了它们,并且它们在蔬菜类中受到保护吗?

标签: c++inheritanceconstructor

解决方案


推荐阅读