You should use the
::
operator to access static members of a class.
The following code compiles (please note your code is anyway not-so-well designed).
If you wish to see a more C++-oriented approach, have a look at the update section below the code.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Movies;
class Movie{
public:
static vector<string> name;
static vector<string> rating;
static vector<int> watched;
friend class Movies;
};
vector<string> Movie::name;
vector<string> Movie::rating;
vector<int> Movie::watched;
class Movies{
public :
void add(string n, string r, int w){
Movie::name.push_back(n);
Movie::rating.push_back(r);
Movie::watched.push_back(w);
}
void inc_watched(string s)
{
for (size_t n=0; n<Movie::name.size(); ++n)
if ( Movie::name[n] == s)
{
Movie::watched[n]++;
break;
}
}
void display(){
cout << " MOVIES " << endl;
cout << "=====================" << endl;
for(size_t x; x<Movie::name.size(); x++){
cout << "NAME : " << Movie::name[x] << endl;
cout << "RATING: " << Movie::rating[x] << endl;
cout << "WATCHED : " << Movie::watched[x] << " Times" << endl;
}
}
};
int main(){
Movies m;
m.add("Movie1", "G", 1);
m.add("Movie2", "PG", 2);
m.add("Movie3", "PG-13", 3);
m.add("Movie4", "R", 4);
m.display();
m.inc_watched("Movie1");
m.inc_watched("Movie1");
m.inc_watched("Movie1");
m.inc_watched("Movie1");
m.display();
}
[update]
A more C++-oriented approach
include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Movie
{
string m_name, m_rating;
int m_watched_count;
public:
Movie( string name, string rating, int watched_count ): m_name(name), m_rating(rating), m_watched_count(watched_count){}
string get_name() const { return m_name; };
void inc_watched(){ ++m_watched_count; }
friend ostream & operator << (ostream & os, const Movie & movie);
};
ostream & operator << (ostream & os, const Movie & movie)
{
os << "NAME : " << movie.m_name << "\n";
os <<"RATING: " << movie.m_rating << "\n";
os << "WATCHED : " << movie.m_watched_count << " Times" << "\n";
return os;
}
void display_movies( const vector<Movie> & movie_vect )
{
cout << " MOVIES \n";
cout << "=====================\n";
for ( const auto & movie : movie_vect)
cout << movie << "---------------------\n";
}
int main()
{
vector <Movie> mv;
mv.push_back(Movie{"Movie1", "G", 1});
mv.push_back(Movie{"Movie2", "PG", 2});
mv.push_back(Movie{"Movie3", "PG-13", 3});
mv.push_back({"Movie4", "R", 4});
display_movies(mv);
auto it = find_if(mv.begin(), mv.end(), [](const Movie & m){ return (m.get_name() == "Movie1");});
if ( it != mv.end())
{
it->inc_watched();
it->inc_watched();
it->inc_watched();
it->inc_watched();
}
display_movies(mv);
}
[/update]