Accessing private members of a class for a non member method/function


Hello,

could you help me fix this error, im not accessing my class private integer. I need your help correctly setting up the object to access my private integer.

Error for compliler



carinventory.cpp:260:4: error: within this context
  a.numOfAmenities = Amen;
    ^
carinventory.cpp:267:36: error: ‘class AutoVehicleType’ has no member named ‘Amen’
                    if (Vehicles[i].Amen == Vehicles[j].Amen )
                                    ^
carinventory.cpp:267:56: error: ‘class AutoVehicleType’ has no member named ‘Amen’
                    if (Vehicles[i].Amen == Vehicles[j].Amen )




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class AutoVehicleType
{ 
        public:
         AutoVehicleType ();
         AutoVehicleType (string brand, int year, string country, int hp, string id, VehicleKind k,
                         string amn[], int num_amn);
         void setAutoInfo(string brand, int year, string country, int hp, string id, VehicleKind vk,
                         string amn[], int num_amn);
        void setAutoBrand(string brand);
        void setYearMade(int yr);
        void setCountryMade(string country);
        void setHoursePower(int hp);
        void setNumPlate(string id);
        void setAutoType(VehicleKind vt);
        string getAutoBrand();
        int getYearMade();
        string getCountryMade();
        int getHoursePower();
        string getNumPlate();
        VehicleKind getAutoType();

        void printVehicle();
        void printVehicleInfo();
        void printAllVehiclesMadeBy(AutoVehicleType[], int noOfVehicles, string brand);
        bool isMadeByCompany(string companyName);
        bool HaveIdenticalAmenities(AutoVehicleType otherVehicles);

        private:
         string autoBrand;
         int yearMade;
         string countryMade;
         int hoursePower;
         string numPlate;
         VehicleKind autoType;
         int numOfAmenities;
         string amenities[4];
         int count;
};



void printVehiclesWithIdenticalAmenities(AutoVehicleType Vehicles[], int noOfVehicles)
{

        AutoVehicleType a;
        int Amen = 0;
        a.numOfAmenities = Amen;
     int i;
        for (i = 0; i < noOfVehicles; i++)
        {

               for (int j = noOfVehicles; j > 0; j--)
               {
                   if (Vehicles[i].Amen == Vehicles[j].Amen )
                   {
                        cout<<"Vehicles "<<Vehicles[i].getNumPlate()<<" and "<<Vehicles[j].getNumPlate()
                        <<" Have Identical set of Amenites";
                   }

               }


        }
}

Last edited on
In your class, the variable you're using for the number of amenities isn't called Amen. It's called numOfAmenities.

Vehicles[i].numOfAmenities == Vehicles[j].numOfAmenities

If you want code from outside the class to be able to read that, make it public in the class:

1
2
public:
      int numOfAmenities;
Last edited on
Well You could make int numOfAmenities ; a public data member but it's not a good practice in general and should not be made habit.

What you could is just a create another function and take the argument of the same type as the numOfAmenities and return it.

Like this :

1
2
3
4
 int get_numOfAmenities ()
{return numOfAmenities}
void set_numOfAmenities(int anynamehere)
{anynamehere = numOfAmenities}


After that you could just call set_numOfAmenities() where you want read that variable.You can also create temporary variable to call upon that function. Post the complete code,If you want to I could implement it.
Last edited on
Topic archived. No new replies allowed.