Error in calling a function for a new object
Apr 1, 2017 at 3:59pm UTC
Hello,
I have created a class to enter, compute and calculate grade averages. When calling the function in int main, I keep getting error messages like
for member 'EnterID_and_Grades' in 'A', which is of non-class type 'Student()'|
I am unsure where I have gone wrong.
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
#include <iostream>
#include<iomanip>
using namespace std;
const int Rows = 2;
const int col = 8;
double IDandGrades [Rows][col];
//int studentID;
class Student{
public :
//vonstructor
Student(double IDandGrades[Rows][col]){
for (int i = 0; i < Rows; i++)
{
for (int j = 0; j < col; j++)
{
IDandGrades[i][j] = 0;
}
}
}
void EnterID_and_Grades(double [Rows][col],int , int );
double StudentGradeAvg(double [Rows][col]);
void ShowIDandGrades(double [Rows][col]);
};
void Student::EnterID_and_Grades(double IDandGradeArray[Rows][col], int studentID1, int studentID2)
{
cout << "Enter the student ID for first student" <<endl;
cin >> studentID1;
IDandGrades[0][0] = studentID1;
cout << "Enter the student ID for 2nd student" <<endl;
cin >> studentID2;
IDandGrades[1][0] = studentID2;
for (int i = 0; i < Rows; i++)
{
for (int j = 1; i < col;j++)
{
cout << "Enter the grade of the student" << endl;
cin >> IDandGradeArray[i][j];
}
}
}
double Student::StudentGradeAvg(double IDandGradeArray[Rows][col])
{
double total = 0;
for (int i = 0; i < Rows; i++)
{
for (int j = 1; j < col-2; j++)
{
total+=IDandGrades[i][j];
}
}
return total / 5;
}
void Student::ShowIDandGrades(double [Rows][col])
{
cout << "Student ID" << setw(5) << "Grade 1" << setw(5) << "Grade 2" << setw(5)
<< "Grade 3" << setw(5) << "Grade 4" << setw(5) << "Grade 5" << endl;
for (int i = 0; i < Rows; i++)
{
cout << endl;
for (int j = 0; j < col; j++)
{
cout << IDandGrades[i][j] << setw(5);
}
}
}
int main()
{
Student A();
Student B();
A.EnterID_and_Grades();
A.ShowIDandGrades();
A.StudentGradeAvg();
return 0;
}
Apr 1, 2017 at 5:06pm UTC
You need to include the constructor parameters in main to meet the requirements of your Student constructor and then the same for the various methods you call in lines 92-93
Without the parameters and a default constructor, A() is not a Student.
Apr 1, 2017 at 5:27pm UTC
You might consider simplifying your class and using the class to hold the grades. That way you don't need to pass them around via the functions. ie a Student 'knows' their grades. You can enter the grades the way I have shown or make it a method of the class as you have done which is just as good. The important thing is the array is a private member of the class and not external to it.
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
#include <string>
#include <iostream>
using namespace std;
class Student{
private :
int grades[10]{0}; // <---
string name;
int ID;
public :
Student(string aName, int anID);
void enterGrade(int aGrade, int anIndex){ grades[anIndex] = aGrade;}
double getAverageGrade(){/* ... */ };
void displayGrades(){/* ... */ };
};
int main()
{
Student A("Smith" , 1056);
A.enterGrade(98, 3);
A.enterGrade(87, 2);
cout << A.getAverageGrade() << endl;
return 0;
}
Apr 8, 2017 at 4:16pm UTC
Hello All,
Thanks very much for the solutions and alternative methodologies. This isn't for any assignments, I'm just curious about coding and picked up a few old Uni textbooks from my science degree.
I'll give these both a go!
Topic archived. No new replies allowed.