我將包含字串的串列的訪問修飾符設定為私有,并引入了一個可以訪問它的公共函式(回傳型別為 void)。嘗試呼叫主函式中的方法時收到錯誤訊息。代碼如下:
class carCompany{
string name;
string owner;
int carsinStock;
list<string> bestsellingCars;
list<string> expensiveCars;
public:
carCompany(string Name,string Owner){//constructor to avoid redundancy when creating objects
Name = name;
Owner = owner;
int carsinStock =0 ;
};
void info(){
cout<<"Autocompany name: "<<name<<endl;
cout<<"Company Owner: "<<owner <<endl;
cout<<"Total cars available: "<<carsinStock<<endl;
cout<<"best selling cars: " <<endl;
for(string bestsellingCars: bestsellingCars){
cout<<bestsellingCars<<endl;
};
cout<<"Expensive cars : "<<endl;
for(string expensiveCars: expensiveCars){
cout<<expensiveCars <<endl;
};}
void importCars() {
carsinStock ;
};
void exportCars()
{carsinStock--;};
void bestsellingcars(string cars){
bestsellingCars.push_back(cars);}
void expensivecars(string cars){
expensiveCars.push_back(cars);}
};
Invoking the function in the main function:
int main(){
carCompany autocomp("Tesla", "Elon Musk");
autocomp.bestsellingcars.push_back("Tesla i");
autocomp.expensivecars.push_back("Tesla ins");
autocomp.info();
carCompany rashidMotors("Rashid motors","Rashid Abu Bakar");
rashidMotors.bestsellingcars.push_back("Porche");
rashidMotors.expensivecars.push_back("Porche carrera");
rashidMotors.info();
return 0;
}
錯誤訊息:invalid use of member'void carCompany::best sellingcars(std::string)'(你忘了'&'嗎?)invalid use of member'void carCompany::expensivecars(std::string)'(確實你忘記了'&'?)成員'void carCompany::best sellingcars(std::string)'的無效使用(你忘記'&'嗎?)成員'void carCompany::expensivecars(std::string)的無效使用)' (你忘了'&'嗎?)
我試圖實作錯誤方法提供的建議,但錯誤仍然存??在。
uj5u.com熱心網友回復:
像這樣稱呼它:
rashidMotors.bestsellingcars("Porche");
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/528761.html
標籤:目的哎呀视觉-C 类方法
上一篇:傳遞類物件陣列
