c++ - 无法使用继承创建参数化构造函数,“不是非静态数据成员或类的基类”
问题描述
当我尝试为我的蔬菜类创建参数化构造函数时,出现此错误:不是非静态数据成员或类的基类。错误发生在实现构造函数的 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_ 不是我的蔬菜类的成员?我不是继承了它们,并且它们在蔬菜类中受到保护吗?
解决方案
推荐阅读
- javascript - 当您在外面释放鼠标时如何停止点击传播
- javascript - 带有路由器重定向的只读页面
- jenkins - 如何使此管道同时用于多次运行?
- python - select first face OpenCV detectMultiScale with Python
- javascript - 如何从 HTML 中的父元素转发子元素?
- xcode - 无法从模型编辑器中删除核心数据实体
- java - static final CharSequence 到 R.string.xxx 如何正确执行?
- c# - 通过 WebApplicationFactory 测试内部 httpClient
- angular - Primeng 多选显示所选值,由特殊字符分隔,而不是默认逗号
- mysql - SELECT 临时表和 SET NULL 重复值