gpt4 book ai didi

c++ - qualified-id 在 '(' token 之前的声明中

转载 作者:行者123 更新时间:2023-11-27 22:49:16 24 4
gpt4 key购买 nike

这是一个疯狂的错误,给我带来了很多麻烦。

#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);

};

关于c++ - qualified-id 在 '(' token 之前的声明中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39191765/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com