-1
   #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 :/

Photon
  • 2,717
  • 1
  • 18
  • 22
  • 1
    What happens/does not happen? What do you mean by " i can`t even initialize Book object"? Compilation error? Linker error? Exception? Coredump? –  Sep 19 '14 at 16:53
  • @Arkadiy: The error is in the source code as a comment. – Dark Falcon Sep 19 '14 at 17:07

4 Answers4

6

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);
Community
  • 1
  • 1
Dark Falcon
  • 43,592
  • 5
  • 83
  • 98
0

I dont known if your compiler is doing this for you, but, probably you need to include the string header

 #include <string>

and, as Dark Falcon said, change your book initialization, from {...} to (...), to compile in compiler pre c++11

Amadeus
  • 10,199
  • 3
  • 25
  • 31
0

In the int main(), you are initializing the constructor using the wrong set of brackets. Use () instead of {}.

Change it to-

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

Hope it solves your problem.

Ish
  • 194
  • 1
  • 2
  • 13
-1

In classic C++, you would allocate a book on the heap using

    Book *eragon = new Book("ISBN: ..." And all your other parameters 

I'm on a tablet and can't copy all your arguments to show exactly

KC-NH
  • 748
  • 3
  • 6
  • Why he/she want to allocate in the heap? Stack is faster – Amadeus Sep 19 '14 at 16:56
  • He's learning C++. It's rare that real world objects such as books get allocated on the stack. I feel it's more important to focus on style than speed for a small program like that. Haven't read the book, but it would be easier to build on that lesson to construct a catalog if he was using heap objects. – KC-NH Sep 19 '14 at 17:06
  • While Im not the one who downvote, I argue that the cubersome of dealing with new/delete is too much to justify allocating in the heap. If it is necessarry to do a heap allocation, better to learn how to use a smart pointer, e.g. std::unique_ptr – Amadeus Sep 19 '14 at 17:10
  • 1
    On the contrary, it is rare to allocate single objects on the heap in C++, especially with bare calls to `new`. At the very least, it is bad practice. – juanchopanza Sep 19 '14 at 17:14
  • I'm not sure what the rules are for down votes. It seems arbitrary to down vote an answer because you don't like the style. The code I posted is one or many correct ways to allocate a Book object on a pre-c11 compiler. – KC-NH Sep 19 '14 at 17:38