Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Passing map from object
    primarykey
    data
    text
    <p>I am building a simple program to 'learn as I go', this program takes a couple of text files and processors them, each line of the first text file is information about a person, and for each line an Object (of type Student (see below)) is created and added to a vector. Within the Student objects is a map that stores that students marks. I have a function within the student class that returns the map when called (or at least thats what im trying to do). Currently this function is:</p> <pre><code>marksType Student::printMarks(){ return marks; } </code></pre> <p>(Where <code>marksType = std::map&lt;string, float&gt;</code>) and <code>marks</code> is the map of type <code>marksType</code>.</p> <p>Then in my main function I have:</p> <pre><code>Student a = *qw; studmarks = a.printMarks(); for (std::map&lt;string, float&gt;::iterator iter = studmarks.begin(); iter != studmarks.end(); iter++){ cout &lt;&lt; "TEST" &lt;&lt; endl; } </code></pre> <p>Where qw is a pointer to a student object and studmarks is of type <code>map&lt;string, float&gt;</code></p> <p>The issue is that the cout doesn't get called so the iterator seems to skip (but the student object does have items in the marks map).</p> <p>Heres the complete Student class</p> <pre><code>#include "Student.h" using namespace std; typedef std::map&lt;string, float&gt; marksType; Student::Student(const string &amp;name, int regNo) : Person(name){ marksType marks; this-&gt;regNo = regNo; } int Student::getRegNo() const{ return regNo; } void Student::addMark(const string&amp; module, float mark){ pair&lt;marksType::iterator,bool&gt; check; check = marks.insert (pair&lt;string,float&gt;(module,mark)); if (check.second==false){ marks[module]=mark; } } float Student::getMark(const string &amp;module) const throw (NoMarkException){ if (marks.find(module) != marks.end()){ return marks.find(module)-&gt;second; } else throw NoMarkException(); } float Student::getAverageMark() const throw (NoMarkException){ if (!marks.empty()){ float avgmark = 0; for (marksType::const_iterator avgit=marks.begin(); avgit!=marks.end(); ++avgit){ avgmark = avgmark + avgit-&gt;second; } avgmark = avgmark/marks.size(); return avgmark; } else throw NoMarkException(); } marksType Student::printMarks(){ return marks; } </code></pre> <p>Oh and below is the part of the main function that adds marks to the students,</p> <pre><code>for (vector&lt;Student&gt;::iterator it = students.begin(); it != students.end(); ++it){ Student b = *it; if (regno == b.getRegNo()){ found = true; b.addMark(module, mark); } } </code></pre> <p>I know this works because when I use the getMark function it does work.</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