Home > Back-end >  invoking a public function that gives access to a private member(list of strings)
invoking a public function that gives access to a private member(list of strings)

Time:11-07

I set the access modifier of a list containing strings to private and introduced a public function that gives access to it(return type void). I got an error message while trying to invoke the method in the main function. code is as:

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

The Error message: invalid use of member'void carCompany::bestsellingcars(std::string)' (did you forget the'&'?) invalid use of member'void carCompany::expensivecars(std::string)' (did you forget the'&'?) invalid use of member'void carCompany::bestsellingcars(std::string)' (did you forget the'&'?) invalid use of member'void carCompany::expensivecars(std::string)' (did you forget the'&'?)

I tried to implement the suggestion provided by the error method but the error was still persistent.

CodePudding user response:

Call it like this:

rashidMotors.bestsellingcars("Porche");
  • Related