C++继承,将成员添加到派生类并重新定义构造函数
C++ inheritance, add member to a derived class and redefine constructor
我对C++有一个菜鸟问题。
我有一本课本:
class Book {
public:
string name;
Author author;
double price;
int qtyInStock;
public:
Book(const string & name,
const Author & author,
double price,
int qtyInStock = 0);
string getName() const;
Author getAuthor() const;
double getPrice() const;
void setPrice(double price);
int getQtyInStock() const;
void setQtyInStock(int qtyInStock);
void print() const;
string getAuthorName() const;
};
使用构造函数:
Book::Book(
const string & name,
const Author & author,
double price,
int qtyInStock)
: name(name), author(author)
{ // Init object reference in member initializer list
// Call setters to validate price and qtyInStock
setPrice(price);
setQtyInStock(qtyInStock);
}
现在我想创建一个从它派生的新类"BookDigital",写如下:
class DigitalBook:public Book
{
public:
string site;
DigitalBook(const string & name,
const Author & author,
double price,
int qtyInStock = 0,
string & site);
};
使用构造函数:
DigitalBook:DigitalBook(
const string & name,
const Author & author,
double price, int qtyInStock,
string & site)
: name(name), author(author) { // Init object reference in member initializer list
// Call setters to validate price and qtyInStock
setPrice(price);
setQtyInStock(qtyInStock);
}
我为电子书添加了会员"网站"
当我编译它时,它显示消息错误:
||=== Build: Debug in Test C++ (compiler: GNU GCC Compiler) ===|
C:UsersaalovisiDesktopTest C++Book.h|34|error: default argument missing for parameter 5 of 'DigitalBook::DigitalBook(const string&, const Author&, double, int, std::string&)'|
C:UsersaalovisiDesktopTest C++Book.cpp|14|error: found ':' in nested-name-specifier, expected '::'|
C:UsersaalovisiDesktopTest C++Book.cpp||In constructor 'DigitalBook::DigitalBook(const string&, const Author&, double, int, std::string&)':|
C:UsersaalovisiDesktopTest C++Book.cpp|15|error: class 'DigitalBook' does not have any field named 'name'|
C:UsersaalovisiDesktopTest C++Book.cpp|15|error: class 'DigitalBook' does not have any field named 'author'|
C:UsersaalovisiDesktopTest C++Book.cpp|15|error: no matching function for call to 'Book::Book()'|
C:UsersaalovisiDesktopTest C++Book.cpp|15|note: candidates are:|
C:UsersaalovisiDesktopTest C++Book.cpp|7|note: Book::Book(const string&, const Author&, double, int)|
C:UsersaalovisiDesktopTest C++Book.cpp|7|note: candidate expects 4 arguments, 0 provided|
C:UsersaalovisiDesktopTest C++Book.h|8|note: Book::Book(const Book&)|
C:UsersaalovisiDesktopTest C++Book.h|8|note: candidate expects 1 argument, 0 provided|
||=== Build failed: 5 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
为什么?谢谢你的帮助
default argument missing for parameter 5
第四个参数有默认参数,但第五个参数没有。如果一个参数具有默认参数,则其后面的所有参数也必须如此。要么qtyInStock
最后一个参数,删除其默认参数,要么为site
提供默认参数。
found ':' in nested-name-specifier, expected '::'
你写的是DigitalBook:DigitalBook
而不是DigitalBook::DigitalBook
.
class 'DigitalBook' does not have any field named 'name'
class 'DigitalBook' does not have any field named 'author'
只能在构造函数的初始化器列表中初始化当前类的成员,而不能初始化基类的成员。与其尝试初始化每个基成员,不如初始化整个基子对象:
DigitalBook::DigitalBook(const string & name, const Author & author, double price, int qtyInStock, string & site)
: Book(name, author, price, qtyInStock), site(site)
{}
没有必要调用setPrice
和setQtyInStock
,假设Book
构造函数对这些参数做了正确的事情。
no matching function for call to 'Book::Book()'
这是因为,由于您没有在初始化器列表中包含 Book
的条目,因此需要对其进行默认初始化;但它没有合适的构造函数。如上所述添加初始化器也可以解决此问题。
如果函数具有某个参数的默认值,则所有其他更进一步的参数也应具有默认值。这是编译器试图在其第一条消息中告诉您的内容。
我认为你的问题是你在构造函数(int qtyInStock = 0)中为第四个参数定义了一个默认值,而不是为第五个参数(字符串和站点)定义了一个默认值。
您应该避免参数 4 的定义,或者为参数 5 提供默认值。
另外,我认为您的构造函数存在问题:DigitalBook::D igitalBook应该调用基类构造函数,我猜
构建一个DigitalBook
对象意味着必须构建一个Book
对象。但是,您没有用于Book
的构造函数(没有参数)
因此,在尝试创建DigitalBook
时,无法构建父类,并且出现错误。
你应该做的是
DigitalBook:DigitalBook(const string & name,
const Author & author,
double price,
int qtyInStock,
string & _site) :
Book(name, author, price, qtyInStock),
site(_site)
{
}
这样,您就可以在派生类的构造函数中指定如何构建它所依赖的固有类的对象
解决了,谢谢:)
class Person
{
public:
std::string m_strName;
int m_nAge;
bool m_bIsMale;
std::string GetName() { return m_strName; }
int GetAge() { return m_nAge; }
bool IsMale() { return m_bIsMale; }
void print() const;
Person(std::string strName = "", int nAge = 0, bool bIsMale = false)
: m_strName(strName), m_nAge(nAge), m_bIsMale(bIsMale)
{
}
};
class BaseballPlayer : public Person
{
public:
double m_dBattingAverage;
int m_nHomeRuns;
BaseballPlayer(double dBattingAverage = 0.0, int nHomeRuns = 0)
: m_dBattingAverage(dBattingAverage), m_nHomeRuns(nHomeRuns)
{
}
};
- 定义C++新的环境变量并在 bat 文件中使用它
- 是否可以使用单个定义定义函数的常量和常规版本?(使用模板,自动,decltype等)
- 从使用概念定义的函数返回新对象
- 如何在C++中为 if 和 else 语句定义新行为
- 如何从 C++ 中的现有模板函数定义新函数
- 自定义分配器,包括放置新案例
- 正在通过const-ref未定义的行为捕获新构造的对象
- 如何在CPropertySheet中定义新颜色
- 谁定义新操作员
- 未定义的符号'fixed_address_empty_string':带有protobuf的新张量流运算
- 新的C ,未定义的参考
- boost :: fibonacci_heap:带有比较器重新定义圆形定义错误的嵌套定义
- 宏,用于定义带有添加前缀的新宏
- 取消脱壳:使用模板在多行类型定义中添加一个尖括号后的新行
- C 通过Typedef定义新类型
- 通过在此指针上放置新位置重新初始化对象时未定义的行为
- 在 ss.clear() 之后使用 ss.str( " ") 用于新定义的字符串流
- Eclipse CDT将方法定义定义到标头文件
- 是标头文件中使用“新”定义的成员指针泄漏
- LEX 程序中的未定义定义