I have a class of students which I store into set in my cpp file. The problem I am having is printing out the actual Student object. I have tried all that I can think of and all I get is either the address of the pointer or a compile error. I have a method in my student class called display that prints out all the information in the format I want it to be.
Here is what I have so far.
.cpp file
#include "Name.h"
#include "Student.h"
#include<iostream>
#include<string>
#include<set>
using namespace std;
int main()
{
Student s1;
set<Student *> s;
while(cin>>s1)
{
s.insert(new Student(s1));
}
for(set<Student *>::const_iterator it = s.begin(); it != s.end(); ++it)
{
&(*it).display(cout);
}
}
Student.h
#ifndef STUDENT_H
#define STUDENT_H
#include<string>
#include<iostream>
#include<map>
#include "Name.h"
typedef std::map<std::string, int> GradeMap;
class Student {
public:
Student(const std::string id="", const Name& name = Name(),const GradeMap & grades = GradeMap()):id_(id),name_(name),grades_(grades){}
Student(const Student& s):id_(s.id_),name_(s.name_),grades_(s.grades_){}
virtual ~Student(){}
friend std::istream& operator>>(std::istream& is, Student& s);
virtual void display(std::ostream& os) const{
os << "ID: " << id_ <<std::endl<< "Name: " << name_ << std::endl;
for(std::map<std::string, int>::const_iterator it = grades_.begin(); it != grades_.end(); ++it)
os<<it->first<<' '<<it->second<<std::endl;
}
private:
std::string id_;
Name name_;
GradeMap grades_;
};
inline std::istream& operator>>(std::istream& is, Student& s)
{
std::string id;
std::string key;
int grade;
int count = 0;
Name name;
if(is>>id>>name>>count){
s.id_ = id;
s.name_ = name;
}
else {
is.setstate(std::ios_base::failbit);
}
for(int i = 0; i < count; i++)
{
if(is>>key>>grade)
{
s.grades_[key] = grade;
}
}
return is;
}
#endif
Name.h
#ifndef NAME_H
#define NAME_H
#include <string>
#include <iostream>
class Name{
public:
explicit Name(const std::string& first = "",const std:: string& last = ""):first_(first),last_(last){}
friend std::ostream& operator<<(std::ostream&, const Name&);
friend std::istream& operator>>(std::istream&, Name&);
private:
std::string first_;
std::string last_;
};
inline std::ostream& operator<<(std::ostream& os, const Name& n){
return os << n.first_<< " " << n.last_;
}
inline std::istream& operator>>(std::istream& is, Name& n){
std::string first,last;
if(is >> first >> last ){
n.first_ = first;
n.last_ = last;
}
else
is.setstate(std::ios_base::failbit);
return is;
}
#endif
also here is the file I using to test this
111111111
john smith
3
comp2510 25
eng2525 60
bio3512 45
222222222
jane doe
2
elex1510 90
comp2510 85
The file is organized like so. The id of the student comes first, then their name, and then the number of courses they have taken follow by that many number of courses plus the grade they got in that course.
My question is how would you print out the actual student object?
In the
forloop:(*it)is aStudent*: