令牌之前声明中的限定 ID '('
qualified-id in declaration before '(' token
这是一个疯狂的错误,给我带来了很多麻烦。
#include <iostream>
using namespace std;
class Book {
private:
int bookid;
char bookname[50];
char authorname[50];
float cost;
public:
void getinfo(void) {
for (int i = 0; i < 5; i++) {
cout << "Enter Book ID" <<endl;
cin >> bookid;
cout << "Enter Book Name" << endl;
cin >> bookname;
cout << "Enter Author Name" << endl;
cin >> authorname;
cout << "Enter Cost" << endl;
cin >> cost;
}
}
void displayinfo(void);
};
int main()
{
Book bk[5];
for (int i = 0; i < 5; i++) {
bk[i].getinfo();
}
void Book::displayinfo() {
for(int i = 0; i < 5; i++) {
cout << bk[i].bookid;
cout << bk[i].bookname;
cout << bk[i].authorname;
cout << bk[i].cost;
}
}
return 0;
}
标题中指出的错误应该在main
中的void Book::displayinfo()行'}'标记之前声明。将函数定义void Book::displayinfo(){}
移出main()
。
class Book{
private:
int bookid;
string bookname; // char bookname[50]; because it can accept book name length more than 50 character.
string authorname; // char authorname[50]; because it can accept authorname length more than 50 character.
float cost;
public:
void getinfo(void){
for(int i =0; i < 5; i++){
cout << "Enter Book ID" <<endl;
cin >> bookid;
cout << "Enter Book Name" << endl;
getline(cin,bookname); // Because book name can have spaces.
cout << "Enter Author Name" << endl;
getline(cin,authorname); // Because author name can have spaces too.
cout << "Enter Cost" << endl;
cin >> cost;
}
}
void displayinfo(void);
};
相关文章:
- .cpp和.h文件中的模板专用化声明
- 未在作用域中声明unordered_map
- 当简单捕获中的标识符显示为参数的声明符 ID 时,没有编译器诊断
- C++需要所有声明的类型说明符和预期的非限定 id(需要调试帮助)
- 错误:'[' 之前预期的非限定 id 和错误:'users'未在此范围内声明
- 显式专用化 - 模板 ID 与任何模板声明都不匹配
- 模板类专用化:模板 ID 与任何模板声明都不匹配
- 模板 ID 与任何模板声明都不匹配
- template-id 与任何模板声明 GNU gcc 编译器都不匹配
- 如何声明ON_COMMAND处理程序的 ID
- 声明中的限定 ID 在"="令牌/对象计数器变量之前
- 解析问题:预期的不合格的ID和语义问题:C 需要所有声明的类型说明符
- 具有依赖限定 id 的类成员使用声明是否应为依赖名称
- 令牌之前声明中的限定 ID '='
- c++模板函数错误:template-id不匹配任何模板声明
- "typename qualified-id"引用非类型参数声明中的类型
- 在限定的声明符-id之后查找名称
- 令牌之前声明中的限定 ID '('
- MFC:已添加资源,但资源 ID 仍有未声明的标识符
- 模板id与任何模板声明都不匹配