I am trying to write a program that stores a list of class objects named GradeBook by using vector, and allows users to add, remove or sort objects in the list by interactively giving commands until the user enters quit. The class GradeBook should contain three private data members: int StudentID, string StudentName, and double Grade (from 0.0 100.0).
Access to these private data members should be implemented through public member functions i.e. set member functions and get member functions.
My current code has been giving me trouble with some errors such as:
'void StudentRecord::add()' is private
and
" 'class StudentRecord' has no member named 'remove'.
" 'class StudentRecord' has no member named 'sort'.
" 'class StudentRecord' has no member named 'print'.
#include <vector>
#include <algorithm>
#include <functional>
#include <iostream>
usingnamespace std;
class StudentRecord{
int id ;
char sname[20] ;
double grade ;
void add(void){
vector <int> v1; // vector container
vector <int>::iterator Iter1; // iterator
int i, n;
printf("Enter the total number of record"); // total records of a students
cin >> n; // push student data in range , id starts from 0
for (i = 0; i <= n; i++){
v1.push_back(i);
}
void remove (){
cout << "Enter the student ID to remove" << endl;
cin >> id1;
for (i = 0; i <= n; i++){
if ( std::find(v1.begin(), v1.end(), id1) != v1.end() )
v1.erase (id1);
else
cout<< "Student not found" << endl;
}
}
void sort (){
sort(v1.begin(), v1.end()); // do the sorting according to the student id
cout<<"\nSorted v1 vector is: ";
for (Iter1 = v1.begin(); Iter1 != v1.end(); Iter1++)
cout<<*Iter1<<" ";
cout<<endl;
sort(v1.begin(), v1.end(), greater<int>()); // to sort in descending order
cout<<"\nRe-sorted (greater than) v1 vector is: ";
for (Iter1 = v1.begin(); Iter1 != v1.end(); Iter1++)
cout<<*Iter1<<" ";
cout<<endl;
}
void print(){
cout << "Original v1 vector is: "; // print the data
for (Iter1 = v1.begin(); Iter1 != v1.end(); Iter1++)
cout<<*Iter1<<" ";
cout<<endl;
}
}
}; // end of class definition
int main(void){
int choice;
StudentRecord s1 ; // creating object
s1.add();
s1.remove();
s1.sort();
s1.print();
return 0;
}
Stick public: on line 8. This will make every class function and class member reachable from outside the class. It's awful design and dangerous but will fix your immediate problem.
Your function remove appears to be inside your function add. This is because your code layout is awful and you have made it hard for you to see what you're doing. Indentation exists for a reason.
I did added Public: and added braces for my for loops, and now I am getting the errors:
" 'StudentRecord' was not declared in this scope"
" Expected ';' before 's1' "
" 's1' was not declared in this scope "
Any idea on how to make my main function at the bottom work properly with the other functions inside? Thanks.
Is there any way you could try to compile my code and see how to adjust it? I can make v1 an int to declare it but I don't think the functions in my int main() work.
- Per the instructions, the data members are to be private. Therefore, public should be at line 14, not at line 8 as Moschops suggested.
- The instructions say the class is to be named GradeBook. Is there a reason you named it StudentReord?
- Move line 16 to line 61. vector<int> v1 belongs in main().
- Move add(), remove(), sort(), print() out of the StudentRecord class. As noted previously, they should not be member functions. They should be stand-alone functions. You're going to need to pass v1 to each of those functions by reference.
If I were doing this, I would create a collection class that contains the vector v1. add(), remove(), sort(), and print() would then be members of the collection class since they all deal with a collection of student records.