好了,所以我現在有點在C++和我的第二個分配的小白,我需要做的類與公共和私營參數等等,等等基本上增變函數不起作用,因爲顯然它們不是const類型的?「這個」說法有const類型,但功能沒有被標記const的
這是頭文件與類:
class Customer {
private:
string PhoneNumber_;
string Name_;
string Address_;
public:
string get_PhoneNumber() const {return PhoneNumber_;} // Accessor
const void set_PhoneNumber(unsigned x) {PhoneNumber_ = x;} // Mutator
string get_Name() const {return Name_;}
const void set_Name(unsigned x) {Name_ = x;}
string get_Address() const {return Address_;}
const void set_Address(unsigned x) {Address_ = x;}
};
// declare the CreateCustomer function prototype with default values
Customer* CreateCustomer(const string& id = BLANK, const string& name = BLANK, const string& address = BLANK);
Customer* CreateCustomer(const string& id, const string& name, const string& address) {
Customer* temp = new Customer();
temp->get_PhoneNumber() = id; // Due to the Accessors and Mutators PhoneNumber, Name and Address are now functions
temp->get_Name() = name;
temp->get_Address() = address;
return temp;
}
這是錯誤我在main.cpp中文件獲得:
cout << "\n\nDear ";
cout << Charge[0].Holder.set_Name() << " (" << Charge[0].Holder.set_PhoneNumber() << ")"; // DisplayCustomer(customer) ;
cout << ",\n" << Charge[0].Holder.set_Address() << "\n\n"
基本上,確切的錯誤信息是:
成員函數'set_Name'不可行:'this'參數的類型爲'const Customer',但函數不是type cons t
它也發生在set_PhoneNumber和set_Address之上。任何幫助將不勝感激!謝謝!
更新:我得到它的工作。謝謝大家幫助我!
返回類型是'const void'!? – Jarod42
如果你想**設置**電話號碼**,CreateCustomer()應該調用'set_PhoneNumber(id)'。它的參數看起來應該是一個「字符串」而不是「無符號」。 – Galik
你的錯誤信息是不言自明的。你不能在const對象上調用非const函數。但是,我看不到什麼是收費或持有人,或者您在哪裏以及如何申報這些實體。 –