C++ '(' 标记前的声明中的限定 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39191765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
qualified-id in declaration before '(' token
提问by Abhishek Mane
This is some crazy error and is giving me a lot of trouble.
这是一些疯狂的错误,给我带来了很多麻烦。
#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;
}
The error, as noted in the title is expected declaration before '}' token at the line void Book::displayinfo() in main
如标题中所述,错误是在 main 中的 void Book::displayinfo() 行的 '}' 标记之前的预期声明
Also this error is coming expected '}' at end of input
此外,此错误在输入结束时预计会出现 '}'
回答by Shravan40
Move the function definition void Book::displayinfo(){}
out of the main()
.
将函数定义void Book::displayinfo(){}
移出main()
.
Along with this, i have some more suggestion for you. Update your class definition like this
除此之外,我还有一些建议给你。像这样更新你的类定义
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);
};