impossible to access the private vector.

I am practicing a little bit in c++; but got some issues concerning private vectors.
I keep getting error message saying that "It is a private member" &&
no member "countryVector " in header2
I checked some other forums, but they all say that i better not us it as a friend class.
is there anything else possible to do, or am i already wrong in my code ?

Country.h
1
2
3
4
5
6
7
8
9
10
11
   public: 
      Country(string nameCountry, string capitalCity, int code);
      void setCountry(string nameCountry);
      void setCity(string capitalCity);
      void setCode(string code);
      void addCountry();
     //+some getters and setters
   private:
      string nameCountry;
      string capitalCity;
      int code;


CountryVect.h
1
2
    private:
        vector<Country>myCountries;

Country.cpp
1
2
3
4
5
6
7
8
9
10
void Country::addCountry(){
    string inCountry; string inCity; int inCode;
        cin>>inCountry; 
        cin>>inCity; 
        cin>>inCode;
    
   Country newCountry(inCountry, inCity, inCode); 
  
   CountryVect::myCountries.push_back(newCountry);
}


UseCountry.cpp
1
2
3
4
5
6
int main(){ 

     Country myCountries(country, city; code);
     myCountries.addCountry();
     return 0;
}


Last edited on
First of all use descriptive names. Header1/Header2 doesn't tell anything about what this classes are used for.

What it looks like is that Header2 is a container for Header1 hence Header1 does not need to know anything about Header2 while Header2 knows everything about Header1, i.e. call its functions.
ok, so, the thing is that i made a mistake, it should be a vector of class, and make the other class as a friend.
Vector2.h should be :
1
2
3
friend class header1;
private:
vector<header1>countryVector;

But still, I always get the message saying that
"Invalid use of a non-static data member 'countryVector'"
Last edited on
You either do not show the entire content of the files or you really should reread:

http://www.cplusplus.com/doc/tutorial/classes/
http://www.cplusplus.com/articles/Gw6AC542/
I updated my first code on the forum. I tried to make it more clear.
but i think (still a beginner) i know what the classes are and the links you posted , i already read them.
thank you though . i am reading them again in case i missed something.

However. the problem is the error message. i dont find where it comes from . and what it means
It is a private member" && no member "myCountries " i
Do you claim that everything that is in the file CountryVect.h is:
1
2
    private:
        vector<string>myCountries;


No other lines?


No #include <vector> or #include <string> in any of the files?


As to the error messages, please copy-paste them verbatim.
they are included of corse.
I just didn't put them to avoid surcharging the message.
But i found the problem now.
I didn't pass my vector to the function addCountry lol
Just change a few lines of code:
1
2
3
4
5
6
7
8
9
10
11
12
   public: 
      Country(string nameCountry, string capitalCity, int code);
      void setCountry(string nameCountry);
      void setCity(string capitalCity);
      void setCode(string code);
      void addCountry();
      void getUserInput();
     //+some getters and setters
   private:
      string nameCountry;
      string capitalCity;
      int code;
1
2
3
4
5
void Country::getUserInput(){
        cin>>nameCountry; 
        cin>>capitalCity; 
        cin>>code;
}
1
2
3
4
    private:
        vector<string>myCountries;
    public:
        void addCountry();
1
2
3
4
5
6
void CountryVect::addCountry(){
   Country newCountry; 
newCountry.getUserInput();
  
   myCountries.push_back(newCountry);
}
1
2
3
4
5
6
int main(){ 

     CountryVect myCountries(country, city; code);
     myCountries.addCountry();
     return 0;
}
Last edited on
Topic archived. No new replies allowed.