调用一个公共函数来访问私有成员(字符串列表)



我将包含字符串的列表的访问修饰符设置为private,并引入了一个公共函数来访问它(返回类型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;
}

错误消息:无效使用成员"void carCompany::bestssellingcars(std::string("(您忘记了"&"吗?(无效使用成员"void carCompany::expensivecars(std::string("(您忘记了"&"吗?(无效使用成员"void carCompany::bestssellingcars(std::string("(您忘记了"&"吗?(无效使用成员"void carCompany::expensivecars(std::string("(您忘记了"&"吗?(

我试图实现error方法提供的建议,但错误仍然存在。

这样称呼它:

rashidMotors.bestsellingcars("Porche");

最新更新