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){