I am trying to learn c++ and I have come across an exercise that is giving me quite a bit of trouble. The compiler is giving me a number of errors, in particular the following:
expected ',' or '...' before '&=' token
Book(int=0, const string&=" ", const string&=" ", int=0, const string&=" ", double=0.0);
Could you help me? I enclose my code.
//Book.h This is the base class
#ifndef BOOK_H
#define BOOK_H
#include <iostream>
#include <string>
#include <stdexcept>
#include <sstream>
#include <vector>
#include <algorithm>
#include "Date.h"
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::ostringstream;
using std::invalid_argument;
using std::out_of_range;
using std::vector;
using std::sort;
class Book{
public:
//Costruttore
Book(int=0, const string&=" ", const string&=" ", int=0, const string&=" ", double=0.0);
virtual ~Book()=default;
//Funzioni Set
void setId(int);
void setAuthors(const string&);
void setTitle(const string&);
void setYear(int);
void setPosition(const string&);
void setValue(double);
//Funzioni Get
int getId() const;
string getAuthors() const;
string getTitle() const;
int getYear() const;
string getPosition() const;
double getValue() const;
//Funzione Stampa
virtual void print() const;
//Overloading di operatori utili per l'ordinamento
bool operator<(const Book& sec) const;
protected:
int book_id, year;
string authors, title, position;
double value;
};
#endif
//Book.cpp, implementation of Book.h
#include "Book.h"
//Costruttore
Book::Book(int id, const string& aut, const string& t, int y, const string& pos, double val){
setId(id);
setAuthors(aut);
setTitle
setYear(y);
setPosition(pos);
setValue(val);
}
//Funzioni SET
void Book::setId(int id){
book_id=id;
}
void Book::setAuthors(const string& aut){
authors=aut;
}
void Book::setTitle(const string& t){
title=t;
}
void Book::setYear(int y){
if(y<=0 || y>2022)
out_of_range("L'anno deve essere compreso fra 1 e 2022");
else
year=y;
}
void Book::setPosition(const string& pos){
position=pos;
}
void Book::setValue(double val){
if(val<0)
out_of_range("Il valore deve essere maggiore o uguale a 0.");
else
value=val;
}
//Funzioni GET
int Book::getId() const{
return this->book_id;
}
string Book::getAuthors() const{
return this->authors;
}
string Book::getTitle() const{
return this->title;
}
int Book::getYear() const{
return this->year;
}
string Book::getPosition() const{
return this->position;
}
double Book::getValue() const{
return this->value;
}
//Funzione STAMPA
void Book::print() const{
cout<<"Nome Libro: "<<getTitle()<<
"nAutori: "<<getAuthors()<<
"nID: "<<getId()<<
"nAnno di pubblicazione: "<<getYear()<<
"nPosizione in libreria: "<<getPosition()<<
"nValore: "<<getValue()<<"$"<<endl;
}
/*
Overloading <
La traccia ci chiede di ordinare per autore, se l'autore
coincide allora bisogna ordinare per anno di pubblicazione.
*/
bool Book::operator<(const Book& sec) const{
if(authors==sec.authors) return this->year<sec.year;
else return this->authors<sec.authors;
}
//Borrow.h This is the derived class
#ifndef BORROW_H
#define BORROW_H
#include "Book.h"
class Borrow : public Book{
public:
//Dovrà avere due costruttori, uno con data di restituzione standard (30 giorni dopo la data di prestito)...
Borrow(int, const string&, const string&, int, const string&, double, const Date&);
virtual ~Borrow()=default;
//Funzioni SET
void setDueDate(const Date&);
//Funzioni GET
Date getDueDate() const;
//...L'altro con data di resituzione diversa
//Borrow(const Book&, Date, Date);
//Operatore < per l'ordinamento
//bool operator<(const Borrow&) const;
//Funzione per la stampa, sarà un override poiché è già presente nella classe base
virtual void print() const override;
protected:
Date collectDate; //Giorno in cui il libro viene preso in prestito
Date dueDate; //Giorno in cui il libro deve essere restituito
};
//Borrow.cpp
#include "Borrow.h"
Borrow::Borrow(int id, const string& aut, const string& t, int y, const string& pos, double val, const Date& dat){
collectDate=dat;
setDueDate(dat);
}
void Borrow::setDueDate(const Date& dat){
for(int i=0; i<30; i++){
dat++;
}
dueDate=dat;
}
/*
Borrow::Borrow(const Book& b, Date datOne, Date datTwo){
collectDate=datOne;
dueDate=datTwo;
}
*/
void Borrow::print() const{
cout<<"Preso in prestito il: "<<collectDate<<endl;
cout<<"Da restituire il: "<<getDueDate()<<endl;
}
Date Borrow::getDueDate() const{
return dueDate;
}
//Makefile
CC=g++
CFLAGS=-std=c++11
DEPS= Book.h Borrow.h Date.h
OBJ= Book.o Borrow.o Date.o main.o
%.o: %.cpp $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
esegui: $(OBJ)
$(CC) -o $@ $^ $(CFLAGS)
clean: $(OBJ)
rm -f *.exe *.o
//Compiler error
In file included from Book.cpp:1:0:
Book.h:24:33: error: expected ',' or '...' before '&=' token
Book(int=0, const string&=" ", const string&=" ", int=0, const string&=" ", double=0.0);
^~
Book.h:24:9: error: default argument missing for parameter 2 of 'Book::Book(int, std::__cxx11::string)'
Book(int=0, const string&=" ", const string&=" ", int=0, const string&=" ", double=0.0);
^~~~
Book.cpp:4:1: error: prototype for 'Book::Book(int, const string&, const string&, int, const string&, double)' does not match any in class 'Book'
Book::Book(int id, const string& aut, const string& t, int y, const string& pos, double val){
^~~~
In file included from Book.cpp:1:0:
Book.h:21:7: error: candidates are: Book::Book(const Book&)
class Book{
^~~~
Book.h:24:9: error: Book::Book(int, std::__cxx11::string)
Book(int=0, const string&=" ", const string&=" ", int=0, const string&=" ", double=0.0);
^~~~
make: *** [Book.o] Error 1