Designing Classes

I keep getting compiler errors. Could anyone help please? Thanks

This is the error message:
F:\DPR226\Homework\Student\main.cpp In function `int main(int, char**)':
39 F:\DPR226\Homework\Student\main.cpp invalid conversion from `int' to `int*'
39 F:\DPR226\Homework\Student\main.cpp initializing argument 4 of `void Student::setStudent(std::string, std::string, std::string, int*)'
F:\DPR226\Homework\Student\Makefile.win [Build Error] [main.o] Error 1

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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// main.cpp
#include <cstdlib>
#include <iostream>
#include "Student.h"

using namespace std;

int main(int argc, char *argv[])
{
              string id;
              string fName;
              string lName;
              int score[SCORE];
              double average;
              char grade;            
              int  numOfStu = 0;
              
             Student student[SIZE];
             
             while(  numOfStu < SIZE && !cin.eof())
                  {   
                    cout << "Enter student " << Student :: getNumOfStu()+1 << " id, enter ctrl-z to quit: ";                   
                    getline(cin, id);   
                    if(!cin.eof())                                      
                    {                  
                       cout << "Enter first name: ";
                       getline(cin, fName);
                       cout << "Enter last name: ";
                       getline(cin, lName);
                      
                       cout << "Enter " << SCORE <<" test score: "; 
                       for (int i = 0; i < SCORE; i++)
                       {
                         cin >> score[i];
                       }                      
                       cin.ignore(); 
                      }                                                                
                       system("cls");       
                      
                       student[numOfStu].setStudent(id, fName, lName, score[SCORE]);
                       student[numOfStu].calculate();
                       student[numOfStu].print(); 
                        numOfStu++;                                             
                   } 
        
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
//Student.h
#ifndef STUDENT
#define STUDENT
#include <string>
using namespace std;

const int SIZE =3, SCORE = 5;
class Student
{
      private:
              string id;
              string fName;
              string lName;
              int score[SCORE];
              double average;
              char grade;
              static int numOfStu;
      public:
             Student();
             Student(string idIn, string fNameIn, string lNameIn,
                    int scoreIn[]);           
             void setStudent(string idIn, string fNameIn, string lNameIn,
                    int scoreIn[]);
             
             string getID() {return id;}
             string getFName() {return fName;}
             string getLName() {return lName;}
             int getScore() {return score[SCORE];}
             
             void calculate();
             
             static int getNumOfStu();
             
             void print() const;
};
#endif
// Student.cpp
#include <cstdlib>
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

#include "Student.h"

int Student :: numOfStu = 0;

Student :: Student()
{
     id = " ";
     fName = " ";
     lName = " ";
     for(int i = 0; i < SCORE; i++)
     {
             score[i] = 0;
     }                              
}
Student :: Student(string idIn, string fNameIn, string lNameIn,
                    int scoreIn[])           
{
        setStudent(idIn, fNameIn, lNameIn, scoreIn[]); 
        numOfStu++;                   
}         
          
void Student :: setStudent(string idIn, string fNameIn, string lNameIn,
                    int scoreIn[])
{
     id = idIn;
     fName = fNameIn;
     lName = lNameIn;
     for(int i = 0; i < SCORE; i++)
     {
             score[i] = scoreIn[i];
     }                                   
}             

             
void Student :: calculate()
{
     int totalScore = 0;
    
     for(int i = 0; i < SCORE; i++)
         totalScore += score[i];
     
     average = totalScore / SCORE; 
     
     if(average >= 90)
       grade = 'A';
     else if(average >= 80)
        grade = 'B';
     else if(average >= 70)
        grade = 'C';
     else if(average >= 60)
        grade = 'D';
     else 
        grade = 'F';   
 }
    
int Student :: getNumOfStu()
{
       return numOfStu;
}

         
      
void Student :: print() const
{
    cout << left << setw(5) << i << setw(6) << fName << setw(6) << lName
        << right << setw(8);
    for(int i = 0; i < SCORE; i++)
         { 
          cout << score[i] << setw(8);
         }    
    cout << fixed << showpoint << setprecision(1);    
         
         cout << average << setw(8) << grade << endl;      
 }

On the line specified by the error, you are passing a single integer in the "score" array, instead of the entire score array as requested by the function. Additionally, you are not even passing a valid integer in the array, since valid subscripts only go from [0] to [SCORE-1].
This is not how you pass the whole array:
 
student[numOfStu].setStudent(id, fName, lName, score[SCORE]);

As Zhuge said it only passes one element of the array, and that element is after the last valid element.. To pass the array you need to specify only the variable name without dereferencing it.
1
2
// pass score without dereferencing it with [] (which selects only one element)
student[numOfStu].setStudent(id, fName, lName, score); 

Last edited on
Thanks Zhuge and Galik, I changed the code according to your suggestion, and this error msg is gone now. Thanks again. : )
Topic archived. No new replies allowed.