vyi
vyi

Reputation: 1092

Learning C++ and got an error with an early example (constructor required before non-static data member)

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!

Upvotes: 3

Views: 251

Answers (1)

dorKKnight
dorKKnight

Reputation: 119

c++ started supporting initialization of non-static members inside a class since c++ 11 onwards. Previous to that non-static members could not be initialized inside a class definition(what the example tries to do here) . g++ version 5.4 supports this feature. Probably you just need to compile with the c++ flag like this (use g++ compiler for c++ and not gcc which is for c language):

$ g++ -std=c++11 your_file.cpp -o your_program

non-static members of a class are specific for that class and every object of the class gets their own individual instance of these members. For more about non-static members please, check-out the following links: cpp ref and this excellent c++ site for beginners

Upvotes: 2

Related Questions