Click here to Skip to main content
15,893,508 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I keep getting these errors

test2.cpp|27|error: expected unqualified-id before '.' token|  
test2.cpp|28|error: expected unqualified-id before '.' token|  
test2.cpp|29|error: expected unqualified-id before '.' token|  
test2.cpp|33|error: expected primary-expression before '.' token|  
test2.cpp|34|error: expected primary-expression before '.' token|  
test2.cpp|35|error: expected unqualified-id before '.' token|  
test2.cpp|41|error: expected primary-expression before '.' token|  
test2.cpp|42|error: expected primary-expression before '.' token|  
test2.cpp|43|error: expected primary-expression before '.' token|  
test2.cpp|44|error: expected primary-expression before '.' token|

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Movies;

class Movie{

    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 n){
        auto it = Movie.name.find(n);
        int x = Movie.watched.begin() - it;
        Movie.watched[x]++;
    }

    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();

}


What I have tried:

I tried putting declaration in different header files for class Movie and Movies and their implementation in .cpp but it didn't work.
Posted
Updated 7-Mar-22 1:19am
Comments
Richard MacCutchan 7-Mar-22 7:06am    
Inside your Movies class you are trying to refer to Movie as an object, but it is a class. Your Movies class should be making a vector of individual Movie items, rather than the way you are trying to do it. You need to rethink your design.

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.

C++
#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
C++"
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]
 
Share this answer
 
v5
Comments
Greg Utas 7-Mar-22 8:42am    
5. I don't have your patience. :)
Maciej Los 7-Mar-22 14:58pm    
5ed!
CPallini 7-Mar-22 15:57pm    
Thank you!
They are static variables, so you'd have to access them as Movie::<member>. However, a static variable in a class means that there's only one instance of it in the entire program, not an instance per object. I think you want to remove the static keyword and the initializations that follow the definition of your class, at least if you want to handle more than one Movie.

In looking at this more closely, you're using a separate static vector for each attribute of a Movie. That could work, but it's not how it's done in C++. Movie shouldn't have any static members, although you could have a global Movies vector to keep track of each individual Movie.
 
Share this answer
 
v2
Comments
CPallini 7-Mar-22 8:40am    
My 5.
Maciej Los 7-Mar-22 14:57pm    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900