Photon Photon - 4 days ago 5
C++ Question

Can`t initialize object using constructor in c++

#include <iostream>
using namespace std;

class Book
{
public:
Book(const string& ISBN,const string& title,const string& author,const string& cprDate,const bool& ch);
void checkBook(void);
void uncheckBook(void);
string ISBN(){return I;};
string title(){return t;};
string author(){return a;};
string cprDate(){return c;};
bool isChecked(){return check;};
private:
string I; //ISBN
string t; //title
string a; //author
string c; //copyright date
bool check; //is checked?
};

Book::Book(const string& ISBN,const string& title,const string& author,const string& cprDate,const bool& ch){
I=ISBN;
t=title;
a=author;
c=cprDate;
check=ch;
}

void Book::checkBook(void)
{
check=true;
}
void Book::uncheckBook(void)
{
check=false;
}
int main()
{
Book eragon{"ISBN:19851654-1851651-156115-156156","Eragonas","Paolini","2007",true};
//^This does not compile, it gives 2 errors: expected primary-expression before eragon
//and expected ';' before semicolon
return 0;
}


I'm doing exercise from book "Programming -- Principles and Practice Using C++" and i`m stuck at chapter 9 exercise 5 :

This exercise and the next few require you to design and implement a Book class, such as you can imagine as part of software for a library. Class Book should have members for the ISBN, title, author, and copyright date. Also store data on whether or not the book is checked out. Create functions for returning those data values. Create functions for checking a book in and out. Do simple validation of data entered into a Book; for example, accept ISBNs only of the form nn-n-x where n is an integer and x is a digit or a letter. Store an ISBN as a string.

and i can`t even initialize Book object :/

Answer

Your compiler is not in C++11 mode. The {...} initializer syntax is new in C++11. Please see this question for enabling C++11 support in CodeBlocks.

The other option is to use C++03 syntax, but if this book is using C++11, you'll probably need to turn it on eventually. The C++03 syntax would be:

Book eragon("ISBN:19851654-1851651-156115-156156","Eragonas","Paolini","2007",true);
Comments