c++有关类的问题,程序出错的原因?用的是VS2015

#include
//#include
#include
//#include

using namespace std;

class Quote
{
public:
Quote() = default;//默认构造函数
Quote(const string &book = "", double sales_price = 0.0) :bookNo(book), price(sales_price)
{
cout << "Quote constuctor is running" << endl;
}
string isbn() const
{
return bookNo;
}
//返回给定数量的书籍销售信息,派生类改写并使用不同的折扣计算方法
virtual double net_price(size_t n)const
{
return n*price;
}
virtual void debug()
{
cout << "bookNo=" << bookNo << "price=" << price << endl;
}
virtual ~Quote()
{
cout << "Quote destructor is running " << endl;
}
friend ostream &operator <<(ostream&, Quote&);
private:
string bookNo;
protected:
double price = 0.0;
};

ostream &operator<<(ostream &os, Quote &e)
{
os << "\tUsing operator<<(ostream &, Quote &)" << endl;
return os;
}

class Bulk_quote :public Quote
{
public:
Bulk_quote(const string &book = "", double sales_price = 0.0, size_t qty = 0,double disc=0.0):
Quote(book,sales_price),min_qty(qty),discount(disc)
{
cout << "Bulk_quote constructor is running" << endl;
}
double net_price(size_t cnt)const
{
if (cnt > min_qty)
return cnt*(1 - discount)*price;
else
return cnt*price;
}
~Bulk_quote()
{
cout << "Bulk_quote deconstructor is running" << endl;
}
private:
size_t min_qty;
double discount;

};//表明的出错部分

ostream &operator<<(ostream &os, Bulk_quote &bq)
{
os << "/tUsing operator <<(ostream&,Bulk_quote&)" << endl;
return os;
}

int main()
{
Quote base("c++ primer", 128.0);
Bulk_quote bulk("Core Python Programing", 89, 5, 0.19);
cout << base << endl;
cout << bulk << endl;
system("pause");
return 0;
}

图片说明

c++

2个回答

Quote() = default;//默认构造函数
Quote(const string &book = "", double sales_price = 0.0) :bookNo(book), price(sales_price)
{
cout << "Quote constuctor is running" << endl;
}

第二个构造函数的两个参数都带有默认参数,就是你不写明参数就用=号后面的默认值。但是俩都不写就跟每一个默认构造函数冲突,产生歧义。
我看你这也用不到默认的,你就把第一行删了吧

Quote() = default;//默认构造函数
Quote(const string &book = "", double sales_price = 0.0) :bookNo(book), price(sales_price)
{
cout << "Quote constuctor is running" << endl;
}
下面的构造函数参数可选,那么对于没有参数的构造函数的调用,编译器怎么知道是调用上面的默认构造函数,还是下面的两个参数但是参数省略的构造函数。
当然出错了。

wendongxia
water_shine 请问您说的下面的构造函数参数可选是什么意思呀?
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!