2016-08-28 7 views
-4

これは、いくつかのクレイジーなエラーであり、私に多くのトラブルを与えている。資格-ID「(」トークン

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

エラー、タイトルに述べたように「}」トークンの前に宣言期待されていますラインボイドブック:: displayinfo()メイン

中でも、このエラーが期待来ている「}」入力

+1

'main'の中に' void Book :: displayinfo() 'を定義することはできません。メンバー関数定義は、クラスが定義されているスコープに属します。この場合、グローバルスコープです。 – StoryTeller

+1

'main()'本体から 'void Book :: displayinfo(){'の定義を移動します。また、書式設定インデントを改善することもできます。 –

+0

非常に興味深い。あなたの質問は何ですか? – juanchopanza

答えて

1

の終わりにmain()のうち、関数定義void Book::displayinfo(){}を移動します。

これに加えて、もう少しあなたの提案があります。このようなクラス定義を更新してください

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

};