How to store an instance of class in a vector? - c++

I have made a class for a student with course and grade, the program keeps asking for a new student until the name given is stop. To store these instances I want to use a vector, but I didn't find any other way to store them than creating an array for the instances first and then pushing them back into the vector.
Is it possible to have room for one instance and delete the values stored in Student student after use so it can be reused?
int i=0;
Student student[20];
vector<Student> students;
cout << "Name?" << endl;
getline(cin,student[i].name);
while((student[i].name) != "stop")
{
student[i].addcoursegrade();
students.push_back(student[i]);
i++;
cout << "Name?" << endl;
getline(cin,student[i].name);
if((student[i].name) == "stop")
break;
};
I also use vectors inside the class to store the values for course and grade, since they are also supposed to be growing. The code for the class is here:
class Student {
public:
string name;
void print() {
cout << name ;
for (int i = 0; i < course.size(); i++)
cout << " - " << course[i] << " - " << grade[i];
cout<<endl;
}
void addcoursegrade() {
string coursee;
string gradee;
cout << "Course?" << endl;
getline(cin, coursee);
course.push_back(coursee);
while (coursee != "stop") {
cout << "Grade?" << endl;
getline(cin, gradee);
grade.push_back(gradee);
cout << "Course?" << endl;
getline(cin, coursee);
if (coursee != "stop")
course.push_back(coursee);
else if(coursee == "stop")
break;
}
};
private:
vector<string> course;
vector<string> grade;
};

Instead of creating an array then pushing back, simply keep one instance around and reassign it:
Student student;
vector<Student> students;
cout << "Name?" << endl;
getline(cin,student.name);
while((student.name) != "stop")
{
student.addcoursegrade();
// this line copies the student in the vector
students.push_back(student);
// then, reassign the temp student to default values
student = {};
cout << "Name?" << endl;
getline(cin,student.name);
if((student.name) == "stop")
break;
};

A few things bothered me:
The way your loops were structured, duplicating the getline. I prefer a while(true) array with a break when the terminating input appears.
No need for a C-style array. std::vector is the way!
separate arrays for course and grade. Instead, I prefer a single record that stores both the course and the grade
indices in your loops that are only used to access the items within the collection. (Just use a range-based for loop)
Don't make a Student object until you need to. Use local variables for string inputs.
As with anything in C++, lots more could be done do to improve it: things like add constructors for your objects, initialize with modern syntax, embrace move semantics, etc. But I'm just making minimal changes.
I'd tackle it like this:
#include <vector>
#include <string>
#include <iostream>
using namespace std;
struct CourseGrade {
string course;
string grade;
};
class Student {
public:
string name;
void print() {
cout << name;
for (auto& courseGrade : courseGrades) {
cout << " - " << courseGrade.course << " - " << courseGrade.grade;
}
cout << endl;
}
void addcoursegrades() {
while (true) {
cout << "Course?" << endl;
string course;
getline(cin, course);
if (course == "stop") break;
cout << "Grade?" << endl;
string grade;
getline(cin, grade);
CourseGrade courseGrade;
courseGrade.course = course;
courseGrade.grade = grade;
courseGrades.push_back(courseGrade);
}
}
private:
vector<CourseGrade> courseGrades;
};
int main() {
vector<Student> students;
while (true) {
cout << "Name?" << endl;
std::string name;
getline(cin, name);
if (name == "stop") break;
Student student;
student.name = name;
student.addcoursegrades();
students.push_back(student);
};
for (auto& student : students) {
student.print();
}
}

Related

i made c++ code where its need to pass structure pointer in a function

i got confused about the structure when i need to to pass the value in a function
#include <iostream>
using namespace std;
struct student
{
int studentID;
char studentName[30];
char nickname[10];
};
void read_student(struct student *);
void display_student(struct student *);
int main()
{
student *s;
//struct student *ps;
read_student(s);
display_student(s);
}
void read_student(struct student *ps)
{
int i;
for (i = 0; i <= 2; i++)
{
cout << "Enter the studentID : ";
cin >> ps->studentID;
cout << "Enter the full name :";
cin.ignore();
cin >> ps->studentName;
cout << "Enter the nickname : ";
cin.ignore();
cin >> ps->nickname;
cout << endl;
}
}
void display_student(struct student *ps)
{
int i;
cout << "student details :" << endl;
for (i = 0; i <= 2; i++)
{
cout << "student name : " << *ps ->studentName << " (" << &ps ->studentID << ")" << endl;
ps++;
}
}
its only problem at the variable at line 19 and when i try to edit it will became more problem
student *s;
//struct student *ps;
//struct student *ps;
read_student(s);
display_student(s);
also can someone explain how to transfer pointer value of structure to the function and return it back to the main function
You are suffering from some leftovers from your C-Language time. And, you have still not understood, how pointer work.
A pointer (as its name says) points to something. In your main, you define a student pointer. But it is not initialized. It points to somewhere. If you read data from somewhere, then it is undefined behavior. For writing, it is the same, plus that the system will most likely crash.
So, define a student. And if you want to give it to a sub-function, take its address with th &-operator (this address will point to the student) and then it will work.
You need also to learn abaout dereferencing. Your output statement is wrong.
Last but not least, if you want to store 3 students, then you need an array or some other storage where to put them . . .
In your read function you always overwrite the previously read student and in your display function you show undetermined data.
Please have a look at your minimal corrected code:
#include <iostream>
using namespace std;
struct student
{
int studentID;
char studentName[30];
char nickname[10];
};
void read_student( student*);
void display_student( student*);
int main()
{
student s;
//struct student *ps;
read_student(&s);
display_student(&s);
}
void read_student( student* ps)
{
cout << "Enter the studentID : ";
cin >> ps->studentID;
cout << "Enter the full name :";
cin.ignore();
cin >> ps->studentName;
cout << "Enter the nickname : ";
cin.ignore();
cin >> ps->nickname;
cout << endl;
}
void display_student( student* ps)
{
cout << "student details :" << endl;
cout << "student name : " << ps->studentName << " (" << ps->studentID << ")" << endl;
ps++;
}
And, the commenters are right. You must read a book.

Is there any way to enter 10 students detail in parameterized constructor and print it using member function with array of object in c++

I tried this code but when I am calling the member function inside the loop it is giving the garbage value of the details and when I am calling the member function outside the loop it is giving me error.
#include<iostream>
#include<string.h>
using namespace std;
class student
{
char name[10];
int id,rollno;
public:
student(char name[10],int id,int rollno)
{
strcpy(this->name,name);
this->id=id;
this->rollno=rollno;
cout<<"the name of the student is:"<<name<<endl;
cout<<"the id of the student is:"<<id<<endl;
cout<<"the roll no of the student is:"<<rollno<<endl;
}
};
int main()
{
int id1,rollno1;
char name1[10];
for(int i=1;i<=2;i++)
{
cout<<" enter the detail of the student "<<i<<" "<<endl;
cout<<"enter the name of the student:";
cin>>name1;
cout<<"enter the id of the student:";
cin>>id1;
cout<<"enter the roll no of the student:";
cin>>rollno1;
student d[]={student(name1,id1,rollno1)};
d[i].print();
}
return 0;
}
Here's your code review.
#include <iostream>
#include <string.h>
using namespace std; // it is strongly suggested that you don't use the whole header unless your intent is speeding up coding
class Student // capitalize classes
{
char _name[10];// it is ok, however, if you use the <string> header, why would you go back to char type?
int _id, _rollno;
public:
Student(char name[10] /* this is just the array's first item passed! */, int id, int rollno)
{
// Don't use this->, use an underscore for one of the names.
// I use underscores for the encapsulated data
strncpy_s(_name, name, 9);// a safer version
_id = id;
_rollno = rollno;
cout << "\nCtor says: ";
cout << "the name of the student is: " << _name << endl;
cout << "the id of the student is: " << _id << endl;
cout << "the roll no of the student is: " << _rollno << endl;
cout << '\n';
}
const char* getName() { return _name; }
const int getId() { return _id; }
const int getRollNo() { return _rollno; }
// unless you overload operator<< , you have to make getters for your info, or make it public
};
int main()
{
int id, rollno;// why 1? they won't intersect with the function
char name[10];
Student *s[2];//you must make that on the heap; otherwise you need a default constructor
for (int i{ 0 }; i < 2; i++)// start with 0 and make it just <
{
cout << "enter the details of the student " << i << endl;// Why so many spaces?
cout << "enter the name of the student: "; // add space after the colon
cin >> name;// remember, you're limited to 9 chars + \n !!!
cout << "enter the id of the student: ";
cin >> id;
cout << "enter the roll no of the student: ";
cin >> rollno;
//student d[] = { student(name,id,rollno) }; // you can't do this. It's not flexible.
// You either assign enough space for the intended number of students in the stack, statically, or
// you lett it find more space in the heap = dynamically
//this goes to the heap
s[i]= new Student( name,id,rollno );// parentheses are well
//s[i] = new Student{ name,id,rollno };// initializer list may also do
//d[i].print();// what's to print here? It's not POD, cout needs instructions
cout << "Stored data -> Id: " <<
s[i]->getId() << ", Name: " // -> dereferences the pointer, and s is a pointer now
<< s[i]->getName() << ", Roll no: "
// or you can dereference it
<< (*s[i]).getRollNo()
<< endl << endl; // make some extra space here
}
return 0;
}

Error:C2679 binary '==': no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion

I have written my code for an employee management system that stores Employee class objects into a vector, I am getting no errors until I try and compile, I am getting the error: C2679 binary '==': no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion). But I am not sure why any help would be great, Thank you!
// Employee Management System
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Employee
{
public:
Employee();
string GetName();
string GetStatus();
float GetSalary();
int GetAge();
int GetYearHired();
private:
string m_Name;
string m_Status;
float m_Salary;
int m_Age;
int m_YearHired;
};
Employee::Employee()
{
m_Salary = 0;
m_Age = 0;
m_YearHired = 0;
}
string Employee::GetName()
{
string fName;
string lName;
cout << "Please enter the new employee's first name: ";
cin >> fName;
cout << "Please enter the new employee's last name: ";
cin >> lName;
m_Name = fName + lName;
return m_Name;
}
string Employee::GetStatus()
{
string status;
cout
<< "Please enter the employee's status (full time, part time, or manager): ";
cin >> status;
return m_Status;
}
float Employee::GetSalary()
{
float salary;
cout << "Please enter the employee's salary: ";
cin >> salary;
return m_Salary;
}
int Employee::GetAge()
{
int age;
while (true)
{
cout << "Please enter the employee's age: ";
cin >> age;
if (age > 0)
break;
else
cout << "Error: Please enter a positive value.";
}
return m_Age;
}
int Employee::GetYearHired()
{
int yearHired;
cout << "Please enter what year the employee was hired: ";
cin >> yearHired;
return m_YearHired;
}
class Staff
{
vector<Employee*> emps;
vector<Employee*>::const_iterator iter;
public:
Staff();
virtual ~Staff();
void Add();
void Remove();
void Clear();
void Display();
};
Staff::Staff()
{
emps.reserve(20);
}
Staff::~Staff()
{
Clear();
}
void Staff::Add()
{
Employee* emp = new Employee;
emp->GetName();
emp->GetStatus();
emp->GetSalary();
emp->GetAge();
emp->GetYearHired();
emps.push_back(emp);
}
void Staff::Remove()
{
Employee* emp;
cout << "Which employee would you like to remove?";
emp->GetName();
iter = find(emps.begin(), emps.end(), emp->GetName()); // Trying to find the employee in the datbase.
if (iter != emps.end()) // If the employee is found in the vector it is removed.
{
cout << "\n" << *iter << " was removed\n\n";
emps.erase(iter); // removes employee from the vector.
}
else // If the employee is not found in the vector, it tells the user that the employee was not found.
{
cout << "Employee not found, please choose anoter employee.\n\n";
}
}
void Staff::Clear()
{
cout << "\nDo you really want to clear all employees? (yes/no)\n"; // Asking the user if they want to clear the database.
string response;
// Storing the response of the user.
cin >> response; // Getting the users response (yes/no).
if (response == "yes") // If response is yes.
{
vector<Employee*>::iterator iter = emps.begin(); // Declares an iterator for the emps vector and sets it to the beginning of the vector.
for (iter = emps.begin(); iter != emps.end(); ++iter) // Iterates through vector.
{
delete *iter; // Deletes the iterators in the vector, freeing all memory on the heap.* iter = 0;
// Sets iterator to zero so it does not become a dangling pointer.
}
emps.clear(); // Clear vector of pointers.
}
else // If response is no.
{
cout << "\nAll employee's remain in the database.\n";
}
}
void Staff::Display()
{
Employee* emp;
if (emps.size() == 0) // Checking to see if the database is empty.
cout
<< "\nThere are no employee's in the database, add employee's to view them here.\n ";
else // If the cart contains any items.
{
cout << "\nThe database contains: \n";
for (iter = emps.begin(); iter != emps.end(); ++iter) // Displaying the inventory.
{
cout << "-------------------------------------------------";
cout << "Employee's Name : " << emp->GetName() << endl;
cout << "Employee's Status : " << emp->GetStatus() << endl;
cout << "Employee's Salary : " << emp->GetSalary() << endl;
cout << "Employee's Age : " << emp->GetAge() << endl;
cout << "Year employee was hired : " << emp->GetYearHired() << endl;
cout << "-------------------------------------------------";
}
}
}
int main()
{
int option = 0;
Staff stf;
// Welcoming the user to the Employee Management System program.
cout
<< "Welcome to our Employee Management System! To get started see the menu options below :\n ";
// Main loop
while (option != 5) // The loop will repeat until the user enters 5 as the option.
{
cout << "------------------------------------------------------------------------------------- - ";
cout << "\nMenu Options: \n";
cout << "\nTo select an option, please type in the number that corresponds to that option.\n ";
cout << "1 - Add an Employee\n2 - Remove an Employee\n3 - Clear the database\n4 - Display Employee's in Database\n5 - Quit" << endl;
cout << "\nWhat would you like to do? ";
cout << "------------------------------------------------------------------------------------- - ";
// Start of the validity check.
bool validInput = false;
while (!validInput) // The loop will repeat until the users input is valid.
{
cin >> option; // User inputs first option choice.
validInput = true; // Assign the input as valid.
if (cin.fail()) // Tests to make sure the value assigned is valid for the variable type.
{
cout << "\nPlease choose a menu option by number\n";
cin.clear(); // Clears stream error.
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Removes an invalid characters.
validInput = false; // Sets the input back to false, repeats the loop.
}
}
switch (option)
{
case 1:
{
stf.Add();
break;
}
case 2:
{
stf.Remove();
break;
}
case 3:
{
stf.Clear();
break;
}
case 4:
{
stf.Display();
break;
}
case 5: // If option = 5.
cout << "\nThank you for using the Employee Management Program!\n";
// Thanks the user for using the Employee Management program.
break;
default: // If the user does not put in a valid option, it tells them to try again.
cout << "\nThat's not a valid option. Please try again.\n";
break;
}
}
system("pause");
return 0;
}
Problem
std::find is going to compare the Employee *s in emps against the std::string that is returned by GetName. There is no comparison operator defined for Employee * that does this. We can make one, but because the behaviour of GetName is to Get and name for the user, not simply return the Employee's name this will quickly become a mess.
Solution
First Stop storing pointers to Employees in the vectors. This simple change will eliminate the vast majority of your past, present and future pain. In general, use new as little as possible (Why should C++ programmers minimize use of 'new'?) and when you do find yourself needing new, prefer a smart pointer.
vector<Employee*> emps;
becomes
vector<Employee> emps;
that has a ripple effect through your code, not the least of which is
void Staff::Add()
{
Employee* emp = new Employee;
emp->GetName();
emp->GetStatus();
emp->GetSalary();
emp->GetAge();
emp->GetYearHired();
emps.push_back(emp);
}
must become
void Staff::Add()
{
Employee emp;
emp.GetName();
emp.GetStatus();
emp.GetSalary();
emp.GetAge();
emp.GetYearHired();
emps.push_back(emp);
}
But also look into emplace_back and strongly consider getting the user input and then constructing emp around it.
bool operator==(const Employee & rhs) const
{
return m_Name == rhs.m_Name;
}
or a friend function
bool operator==(const Employee & lhs,
const Employee & rhs)
{
return lhs.m_Name == rhs.m_Name;
}
and then change the call to find to compare Employees
iter = find(emps.begin(), emps.end(), emp); // Trying to find the employee in the datbase.
This may lead to more problems because iter is a const_iterator and a member variable (Rubber Ducky wants a word with you about this). Also completely ignores the fact that there are a few dozen more logical mistakes in the the code.
It seems to me (EDIT: had a commented out) string response; declaration before
cin >> response; // Getting the users response (yes/no).
Hope this points you in the right direction
EDIT:
It's there but it's commented out. Try:
cout << "\nDo you really want to clear all employees? (yes/no)\n";
// Asking the user if they want to clear the database.
string response;
// Storing the response of the user.
cin >> response; // Getting the users response (yes/no).
if (response == "yes") // If response is yes.
And I'd double check all the code to avoid the comments interfering with the code

C++ do/while loop and calling functions?

I am having trouble trying to get this program to loop if user enter 'y' they'd like to continue. I'm super new to programming by the way so any help is greatly appreciated. I figured the best was to add a do/while in main and as k the user if they'd like to continue at the end of the code but, I quickly realized that wouldn't work unless I called the previous methods for user input and output. That's where the issue is arising.
Thanks again for any help!
/*
ā€¢ Ask the user if they want to enter the data again (y/n).
ā€¢ If ā€™nā€™, then the program ends, otherwise it should clear the student class object and
repeat the loop (ask the user to enter new data...).
*/
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
class Student {
public:
Student();
~Student();
void InputData(); // Input all data from user
void OutputData(); // Output class list to console
void ResetClasses(); // Reset class list
Student& operator =(const Student& rightSide); // Assignment operator
private:
string name;
int numClasses;
string *classList;
};
//array intialized to NULL
Student::Student() {
numClasses = 0;
classList = NULL;
name = "";
}
//Frees up any memory of array
Student::~Student() {
if(classList != NULL) {
delete [] classList;
}
}
// This method deletes the class list
// ======================
void Student::ResetClasses() {
if(classList != NULL) {
delete [ ] classList;
classList = NULL;
}
numClasses = 0;
}
//inputs all data from user (i.e. number of classes)
//using an array to store classes
void Student::InputData() {
int i;
// Resets the class list in case the method
// was called again and array wasn't cleared
ResetClasses();
cout << "Enter student name." << endl;
getline(cin, name);
cout << "Enter number of classes." << endl;
cin >> numClasses;
cin.ignore(2,'\n'); // Discard extra newline
if (numClasses > 0) {
//array to hold number of classes
classList = new string[numClasses];
// Loops through # of classes, inputting name of each into array
for (i=0; i<numClasses; i++) {
cout << "Enter name of class " << (i+1) << endl;
getline(cin, classList[i]);
}
}
cout << endl;
}
// This method outputs the data entered by the user.
void Student::OutputData() {
int i;
cout << "Name: " << name << endl;
cout << "Number of classes: " << numClasses << endl;
for (i=0; i<numClasses; i++) {
cout << " Class " << (i+1) << ":" << classList[i] << endl;
}
cout << endl;
}
/*This method copies a new classlist to target of assignment. If the operator isn't overloaded there would be two references to the same class list.*/
Student& Student::operator =(const Student& rightSide) {
int i;
// Erases the list of classes
ResetClasses();
name = rightSide.name;
numClasses = rightSide.numClasses;
// Copies the list of classes
if (numClasses > 0) {
classList = new string[numClasses];
for (i=0; i<numClasses; i++) {
classList[i] = rightSide.classList[i];
}
}
return *this;
}
//main function
int main() {
char choice;
do {
// Test our code with two student classes
Student s1, s2;
s1.InputData(); // Input data for student 1
cout << "Student 1's data:" << endl;
s1.OutputData(); // Output data for student 1
cout << endl;
s2 = s1;
cout << "Student 2's data after assignment from student 1:" << endl;
s2.OutputData(); // Should output same data as for student 1
s1.ResetClasses();
cout << "Student 1's data after reset:" << endl;
s1.OutputData(); // Should have no classes
cout << "Student 2's data, should still have original classes:" << endl;
s2.OutputData(); // Should still have original classes
cout << endl;
cout << "Would you like to continue? y/n" << endl;
cin >> choice;
if(choice == 'y') {
void InputData(); // Input all data from user
void OutputData(); // Output class list to console
void ResetClasses(); // Reset class list
}
} while(choice == 'y');
return 0;
}
Just get rid of
if(choice == 'y') {
void InputData(); // Input all data from user
void OutputData(); // Output class list to console
void ResetClasses(); // Reset class list
}
Because the variables s1 and s2 are inside the do-while loop, they'll be recreated on each iteration. (The constructor will be called at the definition, and the destructor will be called at the closing brace of the loop, before it tests choice == 'y' and repeats).
The other problem you run into is that your standard input isn't in a state compatible with calling s1.InputData() again. Because you just used the >> extraction operator to read choice, parsing stopped at the first whitespace, and there is (at least) a newline leftover in the buffer. When Student::InputData calls getline, it will find that newline still in the buffer and not wait for additional input.
This is the same reason you used cin.ignore after reading numClasses. You'll want to do the same here.

Sorting names between a Student class

edit. I want to thank the two people who helped me in my code. I fixed my coding as of 7:22 PM PST 9/25/2014. Still having trouble with the sorting though.
So I'm trying to teach myself how to use C++ in order to get ahead of my programming class in school. I've already taken Java so I'm relatively familiar with the structure of the coding but not exactly sure what to be typing. This practice problem is:
1)Ask the user how many people he wants to enter into the list.
2)Read 3 strings, then turn that info into a student class and put the student inside a vector.
3)Sort the list by name. (Extra credit would be to sort the list by Student ID)
4)Print the info of each student in the list.
5)Ask user while answer is 'Y', search the list for a name and print the info correlating to the name
While in Java, this seems pretty easy, I'm having an extremely hard time understanding what is going on in C++.
I currently only have 1, 2, 4 and 5 done. This is what I have so far.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <fstream>
#include <iomanip>
using namespace std;
class Student
{
string myName, myStudentID, myClassID;
public:
Student(string name, string studentID, string classID)
{
myName = name;
myStudentID = studentID;
myClassID = classID;
}
string getName() { return myName; }
string getStudentID() { return myStudentID; }
void printInfo() { cout << myName << setw(15) << myStudentID << setw(10) << myClassID << endl; }
};
int main()
{
int num;
std::vector<Student> studentList;
cout << "How many students do you wish to add to the student list ? " << endl;
cin >> num;
cin.ignore();
for (int a = 0; a < num; a++)
{
string inputName, inputStudentID, inputClassID;
//get name
cout << "Please enter the Student name : ";
getline(cin, inputName);
//get student ID
cout << "Please enter the Student ID number : ";
getline(cin, inputStudentID);
//get class ID
cout << "Please enter the Student class ID : ";
getline(cin, inputClassID);
Student thisStudent(inputName, inputStudentID, inputClassID);
studentList.push_back(thisStudent);
cout << endl;
}
//sort(studentList.begin(), studentList.end());
/*
I never figured out how to sort the list by name.
I do not know how to compare the name of studentList[0] and studentList[1]
to put them in alphabetical order.
*/
cout << endl;; // FORMATTING
cout << "The student list has a size of " << studentList.size() << endl;
cout << endl;; // FORMATTING
cout << "Student Name" << setw(15) << "Student ID" << setw(10) << "Class ID" << endl;
for (int a = 0; a < studentList.size(); a++)
{
studentList[a].printInfo();
}
cout << endl;; // FORMATTING
string searchedName;
char answer;
do
{
cout << endl;; // FORMATTING
cout << "Please type the name of the person you want to search for: ";
getline(cin, searchedName);
for (int a = 0; a < studentList.size(); a++)
{
if (searchedName.compare(studentList[a].getName()) == 0)
{
studentList[a].printInfo();
break;
}
else
if (a == (studentList.size() - 1)) //If went to end of the list, tell them name not found.
cout << "There is no " << searchedName << " in the list. \n";
}
cout << "Would you like to search for another person? \n";
cout << "Y for Yes, N for No. \n";
cin >> answer;
cin.ignore();
} while (answer == 'Y' || answer == 'y');
return 0;
}
You could create a static compare function in class Student to use with std::sort
class Student
{
string myName, myStudentID, myClassID;
// ...
static bool compare_name(const Student & lStudent, const Student & rStudent)
{return (lStudent.myName < rStudent.myName);}
};
// ...
std::sort(studentList.begin(), studentList.end(), Student::compare_name);
or if your compiler supports lambda functions:
std::sort(studentList.begin(), studentList.end(),
[studentList](const Student & lStudent, const Student & rStudent)
{return (lStudent.myName < rStudent.myName);});