2015-12-09 20 views
0

我收到的錯誤是C3867 'Member::getMN': non-standard syntax; use '&' to create a pointer to member'。同樣的錯誤爲getID,nBks,成本和tcost。C++創建書店,語法錯誤

詢問客戶的姓名和身份證號碼,然後查詢他們想要的書籍數量並索要該書的費用。必須顯示所有信息。不知道我的錯誤究竟在哪裏。

#ifndef MEMBER_H 
#define MEMBER_H 
#include <string> 
#include <iostream> 

using namespace std; 

// define the class members and member function prototypes here 
class Member 
{ 
private: 
    string MemberName;//customer name 
    string MemberID;//customer id 
    int numBooks=0;//number of books 
    double PurchaseAmt=0;//cost of books 

public: 
    void setMN(string); 
    void setmID(string); 
    void setnBks(int); 
    void setcost(double); 
    string getMN() const; 
    string getID()const; 
    int nBks()const; 
    double cost()const;//cost of a book 
    double tcost()const; 
}; 
#endif 

#include "member.h" // Needed for the member class 
#include <iostream>  // Needed for cout 
#include <cstdlib>  // Needed for the exit function 
using namespace std; 

void Member::setMN(string name)//string size for membername 
{ if (name.size() < 12) 
     MemberName = name.substr(0, 12); 
    else 
    { 
     cout << "Invalid Member ID\n"; 
     system("pause"); 
     exit(EXIT_FAILURE); 
    } 
} 

void Member::setmID(string ID)//string size for MemberID 
{ if (ID.size() < 5) 
     MemberID = ID.substr(0,5); 
    else 
    { 
     cout << "Invalid Member ID\n"; 
     system("pause"); 
     exit(EXIT_FAILURE); 
    } 

} 
void Member::setnBks(int num)//check for number of books 
{ if (num > 0) 
     numBooks = num; 
    else 
    { 
     cout << "Invalid book count, must be over 0.\n"; 
     system("pause"); 
     exit(EXIT_FAILURE); 
    } 
} 

void Member::setcost(double amount)//check for cost of the book. 
{ if (amount > 0) 
     PurchaseAmt = amount; 
    else 
    { 
     cout << "Invalid cost, can not be less then 0.\n"; 
     system("pause"); 
     exit(EXIT_FAILURE); 
    } 
} 


string Member::getMN() const 
{ return MemberName; 
} 

string Member::getID() const 
{ return MemberID; 
} 

int Member::nBks() const 
{ return numBooks; 
} 

double Member::cost() const 
{ return PurchaseAmt; 
} 

double Member::tcost() const//getting the total cost of the book(s) 
{ return numBooks * numBooks; 

} 

#include <iostream> 
#include <string> 
#include <iomanip> 
#include "member.h" // Needed for Member class 

using namespace std; 

void getinfo(Member&); 
void display(Member); 

int main() 
{ 
    Member books; 

    system("pause"); 
    return 0; 
} // end main 

//this function returns member name and id, also number of books and the cost. 
    // this function displays the book store's data. 
void getinfo(Member& x) 
{ 
    string MemberName, MemberID; 
    int numBooks; 
    double PurchaseAmt; 

    cout << fixed << showpoint << setprecision(2); 

    cout << "What is the members last name, must be under 12 char?\n"; 
    cin >> MemberName; 
    x.setMN(MemberName); 

    cout << "What is the members ID, must be under 5 int?\n"; 
    cin >> MemberID; 
    x.setmID(MemberID); 

    cout << "How many books?\n"; 
    cin >> numBooks; 
    x.setnBks(numBooks); 

    cout << "How much does each book cost?\n"; 
    cin >> PurchaseAmt; 
    x.setcost(PurchaseAmt); 

} // end getting information. 

void display(Member x) 
{ 
    cout << fixed << showpoint << setprecision(2); 
    cout << "The member's last name is : " << x.getMN << endl; 
    cout << "The member's ID is : " << x.getID << endl; 
    cout << "The number of books bought are : " << x.nBks << endl; 
    cout << "The cost per book is : " << x.cost << endl; 
    cout << "The total is : " << x.tcost << endl; 

} 
+1

在發佈有關錯誤的問題,儘量減少代碼只是相關部分(最好是建立一個[最小,完整,可驗證示例](http://stackoverflow.com/help/mcve)向我們展示),並在您顯示的源代碼中標出錯誤行(例如代碼中的註釋)。編譯器會告訴你*確切地說*它發現錯誤的地方,它會一直顯示文件名和行號,有時甚至會顯示函數名。 –

+0

會做,我很抱歉大量的代碼。 – kobe

回答

1

你剛纔忘了()方法名稱後

void display(Member x) 
{ 
    cout << fixed << showpoint << setprecision(2); 
    cout << "The member's last name is : " << x.getMN() << endl; 
    cout << "The member's ID is : " << x.getID() << endl; 
    cout << "The number of books bought are : " << x.nBks() << endl; 
    cout << "The cost per book is : " << x.cost() << endl; 
    cout << "The total is : " << x.tcost() << endl; 

} 
+0

哦哇..我不相信我錯過了..非常感謝你 – kobe