dimanche 26 juin 2016

Can we use default constructor to assigned to a variable in C++?

I read C++ primer(5th edition) and find the code.

class Sales_data
{
friend std::istream &operator>>(std::istream &is,Sales_data &item);
friend std::ostream &operator<<(std::ostream &os, const Sales_data &item);
public:
    Sales_data();
    Sales_data(const std::string &s ,unsigned n ,double p)
        :bookNo(s),units_sold(n),revenue(p*n){ }
private:
    std::string bookNo;
    unsigned units_sold=0;
   double revenue=0.0;
};
std::istream &operator>>(std::istream &is, Sales_data &item)
{
    double price;
    is>>item.bookNo>>item.units_sold>>price;
    if(is)
        item.revenue=item.units_sold*price;
    else
        item=Sales_data(); //I find an error on VS2010
    return is;
}
//...

But when I try it on VS 2010 ,I find that " item=Sales_data(); " was wrong.Why?Is it because it can only be used in C++11?

Aucun commentaire:

Enregistrer un commentaire