Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I print from a set of object pointers in C++?
    primarykey
    data
    text
    <p>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.</p> <p>Here is what I have so far.</p> <p>.cpp file</p> <pre><code>#include "Name.h" #include "Student.h" #include&lt;iostream&gt; #include&lt;string&gt; #include&lt;set&gt; using namespace std; int main() { Student s1; set&lt;Student *&gt; s; while(cin&gt;&gt;s1) { s.insert(new Student(s1)); } for(set&lt;Student *&gt;::const_iterator it = s.begin(); it != s.end(); ++it) { &amp;(*it).display(cout); } } </code></pre> <p>Student.h</p> <pre><code>#ifndef STUDENT_H #define STUDENT_H #include&lt;string&gt; #include&lt;iostream&gt; #include&lt;map&gt; #include "Name.h" typedef std::map&lt;std::string, int&gt; GradeMap; class Student { public: Student(const std::string id="", const Name&amp; name = Name(),const GradeMap &amp; grades = GradeMap()):id_(id),name_(name),grades_(grades){} Student(const Student&amp; s):id_(s.id_),name_(s.name_),grades_(s.grades_){} virtual ~Student(){} friend std::istream&amp; operator&gt;&gt;(std::istream&amp; is, Student&amp; s); virtual void display(std::ostream&amp; os) const{ os &lt;&lt; "ID: " &lt;&lt; id_ &lt;&lt;std::endl&lt;&lt; "Name: " &lt;&lt; name_ &lt;&lt; std::endl; for(std::map&lt;std::string, int&gt;::const_iterator it = grades_.begin(); it != grades_.end(); ++it) os&lt;&lt;it-&gt;first&lt;&lt;' '&lt;&lt;it-&gt;second&lt;&lt;std::endl; } private: std::string id_; Name name_; GradeMap grades_; }; inline std::istream&amp; operator&gt;&gt;(std::istream&amp; is, Student&amp; s) { std::string id; std::string key; int grade; int count = 0; Name name; if(is&gt;&gt;id&gt;&gt;name&gt;&gt;count){ s.id_ = id; s.name_ = name; } else { is.setstate(std::ios_base::failbit); } for(int i = 0; i &lt; count; i++) { if(is&gt;&gt;key&gt;&gt;grade) { s.grades_[key] = grade; } } return is; } #endif </code></pre> <p>Name.h</p> <pre><code>#ifndef NAME_H #define NAME_H #include &lt;string&gt; #include &lt;iostream&gt; class Name{ public: explicit Name(const std::string&amp; first = "",const std:: string&amp; last = ""):first_(first),last_(last){} friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp;, const Name&amp;); friend std::istream&amp; operator&gt;&gt;(std::istream&amp;, Name&amp;); private: std::string first_; std::string last_; }; inline std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, const Name&amp; n){ return os &lt;&lt; n.first_&lt;&lt; " " &lt;&lt; n.last_; } inline std::istream&amp; operator&gt;&gt;(std::istream&amp; is, Name&amp; n){ std::string first,last; if(is &gt;&gt; first &gt;&gt; last ){ n.first_ = first; n.last_ = last; } else is.setstate(std::ios_base::failbit); return is; } #endif </code></pre> <p>also here is the file I using to test this</p> <pre><code>111111111 john smith 3 comp2510 25 eng2525 60 bio3512 45 222222222 jane doe 2 elex1510 90 comp2510 85 </code></pre> <p>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.</p> <p>My question is how would you print out the actual student object?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload