Trouble declaring a user

I keep getting this syntax error for my code: __tester__.cpp: In function ‘int getCountWatchedMovies(std::__cxx11::string, User*, int, int)’:
__tester__.cpp:123:12: error: ‘user’ was not declared in this scope
if(user[i].username == username){

I don't Know what is wrong with my "user[i]" and why is not declared. Here's my code:

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>

using namespace std;

class Movie
{
        private:
                string title,year;
                
        public:
                Movie();
                string getTitle();
                string getReleaseYear();
                void setTitle(string title);
                void setReleaseYear(string year);
};

Movie::Movie()
{
        this->title = "Unknown";
        this->year  = "Unknown";
}

string Movie::getTitle()
{
        return (this->title);
}

string Movie::getReleaseYear()
{
        return (this->year);
}

void Movie::setTitle(string title)
{
        this->title = title;
}

void Movie::setReleaseYear(string year)
{
        this->year  = year;
}

#define MAX 64

class User
{
        private:
                string username;
                int numRatings,ratings[MAX];
                
        public:
                User();
                string getUsername();
                int getNumRatings();
                int getRatingAt(int ind);
                void setUsername(string username);
                void setNumRatings(int numRatings);
                void setRatingAt(int ind,int rating);
};

User::User()
{
        this->username   = "Unknown";
        this->numRatings = 0;
}

string User::getUsername()
{
        return (this->username);
}

int User::getNumRatings()
{
        return (this->numRatings);
}

int User::getRatingAt(int ind)
{
        if(ind>=0 && ind<numRatings)
        {
                return (this->ratings[ind]);
        }
        else
        {
                return 0;
        }
}

void User::setUsername(string username)
{
        this->username   = username;
        
}

void User::setNumRatings(int numRatings)
{
        this->numRatings = numRatings;
}

void User::setRatingAt(int ind,int rating)
{
        if(ind>=0 && ind<numRatings)
        {
                ratings[ind] = rating;
        }
        else
        {
                ratings[ind] = 0;
        }
};

HERE IS WHERE THE PROBLEM IS:

int getCountWatchedMovies(string username,User users[],int numUsersStored,int numMoviesStored){
    for(int i=0;i<numUsersStored;i++){
        //means, user exits
        if(user[i].username == username){
            int count = 0; //number of movies watched
            for(int j=0;j<numMoviesStored;){
                //means user have watched movie/rated
                if(user[i].ratings[j] > 0)
                count++; //so increase count
            }

            //now return count (number of movies watched)
            return count;
        }
    }

    //means no user found
    return -3;
}
Last edited on
Please edit your post and add the formatting tags "[code]" and "[/code]" around your program.

Your array of Users in your getCountWatchedMovies function is named 'users', not 'user'.
Last edited on
Thank you, but after I fixed that I get these:

Syntax Error(s)
__tester__.cpp: In function ‘int getCountWatchedMovies(std::__cxx11::string, User*, int, int)’:
__tester__.cpp:57:24: error: ‘std::__cxx11::string User::username’ is private
string username;
^
__tester__.cpp:123:21: error: within this context
if(users[i].username == username){
^
__tester__.cpp:58:43: error: ‘int User::ratings [64]’ is private
int numRatings,ratings[MAX];
^
__tester__.cpp:127:29: error: within this context
if(users[i].ratings[j] > 0)
^
Last edited on
getCountWatchedMovies is not a member of your User class, therefore it can not access private members of the class.
Thank you so much!! It runs now, although it gives me -3 for everything lol
but thank you, it was very helpful!
My getter/setter rant:
I'm not a fan of getters and setters when used to essentially make every private variable public. If you're going to do that, you might as well make the variables public and eliminate the getters and setters entirely.

Don't get me wrong, I'm a big believer in private variables. Private variables should be manipulated by functions that implement specific ACTIONs that are relevant to your class.
Getting or setting a variable is rarely an action that is relevant to what your class is representing.

If you find yourself blindly writing getters and setters, then you need to step back and think about what actions are performed on your class and how you're going to implement those actions.

p.s. I know it's popular these days to teach writing getters and setters, but from my point of view, blandly writing them to make private variables public is a concept that should be quickly unlearned.
Topic archived. No new replies allowed.