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