C ++继承,将成员添加到派生类并重新定义构造函数

时间:2014-11-26 16:18:00

标签: c++ class inheritance member derived

我对C ++有一个noob问题。

我有一本课程书:

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:\Users\aalovisi\Desktop\Test C++\Book.h|34|error: default argument missing for parameter 5 of 'DigitalBook::DigitalBook(const string&, const Author&, double, int, std::string&)'|
C:\Users\aalovisi\Desktop\Test C++\Book.cpp|14|error: found ':' in nested-name-specifier, expected '::'|
C:\Users\aalovisi\Desktop\Test C++\Book.cpp||In constructor 'DigitalBook::DigitalBook(const string&, const Author&, double, int, std::string&)':|
C:\Users\aalovisi\Desktop\Test C++\Book.cpp|15|error: class 'DigitalBook' does not have any field named 'name'|
C:\Users\aalovisi\Desktop\Test C++\Book.cpp|15|error: class 'DigitalBook' does not have any field named 'author'|
C:\Users\aalovisi\Desktop\Test C++\Book.cpp|15|error: no matching function for call to 'Book::Book()'|
C:\Users\aalovisi\Desktop\Test C++\Book.cpp|15|note: candidates are:|
C:\Users\aalovisi\Desktop\Test C++\Book.cpp|7|note: Book::Book(const string&, const Author&, double, int)|
C:\Users\aalovisi\Desktop\Test C++\Book.cpp|7|note:   candidate expects 4 arguments, 0 provided|
C:\Users\aalovisi\Desktop\Test C++\Book.h|8|note: Book::Book(const Book&)|
C:\Users\aalovisi\Desktop\Test 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)) ===|

为什么?谢谢你的帮助

5 个答案:

答案 0 :(得分:2)

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构造函数使用这些参数做正确的事情,则无需调用setQtyInStockBook

no matching function for call to 'Book::Book()'

这是因为,由于您没有在初始化列表中包含Book的条目,因此需要默认初始化;但它没有合适的构造函数。如上所述添加初始化程序也可以解决此问题。

答案 1 :(得分:0)

如果函数具有某个参数的默认值,那么所有其他参数也应具有默认值。这是编译器在第一条消息中试图告诉你的内容。

答案 2 :(得分:0)

我认为你的问题是你在构造函数中定义了第四个参数的默认值(int qtyInStock = 0),而不是第五个参数(字符串和站点)。

您应该避免参数4的defalut或为参数5提供默认值。

另外我认为你的构造函数有问题:DigitalBook :: DigitalBook应该调用基类构造函数,我想

答案 3 :(得分:0)

建立DigitalBook对象意味着必须构建Book对象。 但是,您没有Book的defaut构造函数(没有参数)

因此,在尝试创建DigitalBook时,无法构建父类,并且您会收到错误。

你应该做的是

DigitalBook:DigitalBook(const string & name,
                        const Author & author,
                        double price,
                        int qtyInStock,
                        string & _site) :
     Book(name, author, price, qtyInStock),
     site(_site)
{
}

这样你就可以在派生类的构造函数中指定如何构建它依赖的固有类对象

答案 4 :(得分:0)

已解决此问题,谢谢:)

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)
{
}
};