Following the book: C++ Primer Stanley B Lippman, Josee Lajoie, Barbara Moo 5th Edition
Section 1.5 has this code to demonstrate standard operations (>> istream and << ostream) with an object of type Sales_item
#include <iostream>
#include "Sales_item.h"
int main(){
Sales_item book;
cin>> book;
cout<< book << endl;
return 0;
}
The file Sales_item.h is available from a download link given in the book.
I am using following CMake to compile my source ISBN0.cpp :
cmake_minimum_required(VERSION 3.0)
add_executable(readISBN ISBN0.cpp)
While compiling I am getting the following error
Sales_item2.h:11:7: error: constructor required before non-static data member for ‘Sales_item::units_sold’ has been parsed
I have tried to cut down the original Sales_item.h into following to reproduce the error :
#ifndef SALESITEM_H
#define SALESITEM_H
#include <iostream>
#include <string>
class Sales_item {
friend std::ostream& operator<<(std::ostream&, const Sales_item&);
friend std::istream& operator>>(std::istream&, Sales_item&);
// private members as before
private:
std::string bookNo; // implicitly initialized to the empty string
unsigned units_sold = 0; // explicitly initialized
double revenue = 0.0;
public:
// constructors are explained in section 7.1.4, pages 262 - 265
// default constructor needed to initialize members of built-in type
Sales_item() = default;
Sales_item(const std::string &book): bookNo(book) { }
Sales_item(std::istream &is) { is >> *this; }
public:
// operations on Sales_item objects
// member binary operator: left-hand operand bound to implicit this pointer
Sales_item& operator+=(const Sales_item&);
// operations on Sales_item objects
std::string isbn() const { return bookNo; }
double avg_price() const;
};
std::ostream&
operator<<(std::ostream& out, const Sales_item& s)
{ ... }
std::istream&
operator>>(std::istream& in, Sales_item& s)
{ ... }
Can someone explain 1: What the error trying to tell? (in simple words) & 2: What modification is required to make the example work?
It is so dis-heartening to see a beginner example not work!