Been working on this for hours now. but most of it has been trying to fix these two errors. I've been googling my heart out and keep getting cryptic answers about the const. Really need help. it's supposed to take in names of students and their scores, sort them, then display. if i comment out the sort function call it builds and runs fine...but with sort uncommented i get this error:
**** Build of configuration Debug for project StudentScores ****
**** Internal Builder is used for build ****
g++ -O0 -g3 -Wall -c -fmessage-length=0 -osrc/Student.o ../src/Student.cpp
../src/Student.cpp: In function ‘bool operator<(const Student&, const Student&)’:
../src/Student.cpp:25: error: passing ‘const Student’ as ‘this’ argument of ‘int Student::getscore()’ discards qualifiers
../src/Student.cpp:25: error: passing ‘const Student’ as ‘this’ argument of ‘int Student::getscore()’ discards qualifiers
Build error occurred, build is stopped
Time consumed: 161 ms.
Here are the three project files. and in case it helps, i'm using eclipse CDT on ubuntu.
/*
* StudentDriver.cpp
*
* Created on: Jun 1, 2010
* Author: rootchord
*/
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
#include "Student.h"
int size;
int main(){
vector<Student> roster;
cout << "How many students do you want to enter?" << endl;
cin >> size;
Student input[size];
string _name;
int _score;
for(int i=0;i<size;i++){
cout << "Enter student name:" << endl;
cin >> _name;
input[i].setname(_name);
cout << "Enter the student's score:" << endl;
cin >> _score;
input[i].setscore(_score);
roster.push_back(input[i]);
}
sort(roster.begin(), roster.end());
vector<Student>::iterator p = roster.begin();
for(int i=0;i<size;i++){
cout << p->getname()<< " got a score of " << p->getscore() << endl;p++;
}
return 0;
}
/*
* Student.h
*
* Created on: Jun 1, 2010
* Author: rootchord
*/
#ifndef STUDENT_H_
#define STUDENT_H_
class Student {
public:
void setname(string);
void setscore(int);
string getname();
int getscore();
friend bool operator <(const Student&, const Student&);
private:
string name;
int score;
};
#endif /* STUDENT_H_ */
/*
* Student.cpp
*
* Created on: Jun 1, 2010
* Author: rootchord
*/
#include<string>
using namespace std;
#include "Student.h"
void Student::setname(string _name){
name = _name;
}
void Student::setscore(int _score){
score = _score;
}
string Student::getname(){return name;}
int Student::getscore(){return score;}
bool operator< (const Student& first, const Student& second){
return (first.getscore() < second.getscore());
}