Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;预期的';)';参数名之前 我对C++很陌生。我试图创建一个名为Book的基本类,但我的.cpp文件中出现了一个错误:_C++ - Fatal编程技术网

C++;预期的';)';参数名之前 我对C++很陌生。我试图创建一个名为Book的基本类,但我的.cpp文件中出现了一个错误:

C++;预期的';)';参数名之前 我对C++很陌生。我试图创建一个名为Book的基本类,但我的.cpp文件中出现了一个错误:,c++,C++,错误:“作者”之前应为“)” 然后“作者”、“标题”和“页面”都会说: 无法解析标识符 我在网上看了很多例子,我看不出我做错了什么 Book.h: #include <string> using namespace std; #ifndef BOOK_H #define BOOK_H class Book{ public: string author; string title; int pages; Book(string theAuthor,

错误:“作者”之前应为“)”

然后“作者”、“标题”和“页面”都会说:

无法解析标识符

我在网上看了很多例子,我看不出我做错了什么

Book.h

#include <string>
using namespace std;

#ifndef BOOK_H
#define BOOK_H

class Book{
public:
    string author;
    string title;
    int pages;

    Book(string theAuthor,string theTitle,int thePages);
};
#endif  /* BOOK_H */
更改:

Book(string theAuthor,string theTitle,int thePages){
    author = theAuthor;
    title=theTitle;
    pages=thePages;
}
致:


您需要这样声明:

Book::Book(string theAuthor,string theTitle,int thePages){

通过这种方式,编译器知道您正试图实现类“Book”(这就是“Book::”的意思)的构造函数(这就是第二个“Book(…)”的目的)

您已经在类内部声明了
Book
的构造函数,但是您将定义放在了外部。该定义不是自由函数,因此必须同时命名要定义的类和类的方法。对于构造函数,这些名称是相同的。所以:
Book::Book


类之外的析构函数定义应该是
Book::~Book
,但是默认的析构函数通常是可以的。

谢谢!这是我读过的例子,但直到end@MattPashby没有问题这不是声明,这是定义,声明在头文件中。
Book::Book(string theAuthor,string theTitle,int thePages){
    author = theAuthor;
    title=theTitle;
    pages=thePages;
}
Book::Book(string theAuthor,string theTitle,int thePages){