class构造函数初始化调用问题

发布网友

我来回答

1个回答

热心网友

#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Screen{
public:
typedef string::size_type index;
Screen(){}//在这里加一个默认构造函数就行了。
Screen(index hght,index wth):height(hght),width(wth){ }
char get() const { return contents[cursor]; }
char get(index ht,index wd ) const;
private:
string contents;
index cursor;
index height,width;
};
class Sales_item{
public:
double avg_price() const;
bool same_isbn(const Sales_item &rhs)const{
return isbn == rhs.isbn;
}
Sales_item():unite_sold(0),revenue(0.0) { }
private:
string isbn;
unsigned unite_sold;
double revenue;
};
inline double Sales_item::avg_price() const{
if(unite_sold)
return revenue/unite_sold;
else
return 0;
}
int main()
{
Sales_item cprimer,primer;
cprimer.same_isbn(primer);
cout<<cprimer.avg_price();
Screen windows;//error没有合适的默认构造函数可用
return 0;
}

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com