简体   繁体   English

无法使用C ++中的构造函数初始化对象

[英]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 : 我正在从“编程-使用C ++的原理和实践”这本书中进行练习,而我被困在第9章练习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. 本练习以及接下来的几个练习要求您设计和实现Book类,例如您可以想象作为图书馆软件的一部分。 Class Book should have members for the ISBN, title, author, and copyright date. 课堂书籍应具有ISBN,标题,作者和版权日期的成员。 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; 对输入到Book中的数据进行简单验证; for example, accept ISBNs only of the form nn-nx where n is an integer and x is a digit or a letter. 例如,仅接受nn-nx形式的ISBN,其中n是整数,x是数字或字母。 Store an ISBN as a string. 将ISBN存储为字符串。

and i can`t even initialize Book object :/ 而且我什至不能初始化Book对象:/

Your compiler is not in C++11 mode. 您的编译器不在C ++ 11模式下。 The {...} initializer syntax is new in C++11. {...}初始化程序语法是C ++ 11中的新增功能。 Please see this question for enabling C++11 support in CodeBlocks . 请参阅此问题以在CodeBlocks中启用C ++ 11支持

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. 另一种选择是使用C ++ 03语法,但是如果本书使用的是C ++ 11,则最终可能需要将其打开。 The C++03 syntax would be: C ++ 03语法为:

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

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 并如Dark Falcon所说,将您的书籍初始化从{...}更改为(...),以在c ++ 11之前的编译器中进行编译

In the int main(), you are initializing the constructor using the wrong set of brackets. 在int main()中,您使用错误的括号集来初始化构造函数。 Use () instead of {}. 使用()代替{}。

Change it to- 更改为-

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

Hope it solves your problem. 希望它能解决您的问题。

In classic C++, you would allocate a book on the heap using 在经典C ++中,您将使用以下方法在堆上分配一本书

    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 我在平板电脑上,无法复制所有参数以准确显示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM