Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

qualified-id in declaration before '(' token

Tags:

c++

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

Also this error is coming expected '}' at end of input

like image 422
Abhishek Mane Avatar asked Aug 28 '16 13:08

Abhishek Mane


1 Answers

Move the function definition void Book::displayinfo(){} out of the 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);

};
like image 151
Shravan40 Avatar answered Nov 20 '22 07:11

Shravan40