Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As @dasblinkenlight already pointed out, you need/want to pass streams by reference.</p> <p>That's just the tip of the iceberg though. Your code has a number of more pernicious problems. Chief among them is code like:</p> <pre><code>while(!(inFile.eof())) </code></pre> <p>Code like this is broken. It won't work. It never has and it never will (short of some major miracle occurring). It's barely possible to put a condition inside the loop (with a break statement) to get out of the loop at the correct time -- but you haven't done so, and almost nobody else does either, and when you do, you might as well turn the loop itself into <code>while (true)</code>, because it'll always be that <em>other</em> logic that exits the loop at the right time -- because this loop condition can't and won't. [I don't mean to sound harsh or nasty there, just trying to be completely clear that this code absolutely does <em>not</em> work.]</p> <p>You have the same basic problem again here:</p> <pre><code> while(!(in.eof())&amp;&amp;(readline != " " || readline != "/n")) </code></pre> <p>This adds what I'd guess is another (though less common and much easier to fix) problem -- your <code>"/n"</code> was almost certainly intended to be <code>"\n"</code>.</p> <p>In most cases, what you really want/need to do is read something from the stream, and have the function that does the reading return a reference to the stream. This will let you read items until the reading fails (at which point the stream's failbit should be set). Once you've done that, you can make the loop conditional on the reading succeeding. In <em>most</em> cases, it's convenient to name that function <code>operator&gt;&gt;</code>, so reading an object of your class uses the same syntax as reading things like ints.</p> <p>For example, let's take a look at your <code>parseTeam</code>:</p> <pre><code> void CTeam::ParseTeam(std::istream in){ string readline; getline(in, readline); this-&gt;TeamName = readline; while(!(in.eof())&amp;&amp;(readline != " " || readline != "/n")) { getline(in, readline); this-&gt;Parse(readline); } } </code></pre> <p>If it were up to me, I'd write something more like this:</p> <pre><code>std::istream &amp;operator&gt;&gt;(std::istream &amp;is, CTeam &amp;t) { std::getline(is, t.Teamname); CTeam::member member; while (is &gt;&gt; member) t.members.push_back(member); return is; } </code></pre> <p>Then, <code>CTeam</code> would have something along this general line:</p> <pre><code>class CTeam { // ... public: class member { // ... friend std::istream &amp;operator&gt;&gt;(std::istream &amp;is, member &amp;m) { // code to read one team member from is } }; }; </code></pre> <p>I should also add that without knowing more about the program, that organization definitely is <em>not</em> cast in concrete -- for example, it's not clear whether the entity I've shown as <code>CTeam::member</code> might actually be able to exist outside the context of a team. If it can, then you (almost certainly) want to make it an independent class of its own, outside of CTeam. For the moment, I'm simply guessing at how to organize the code based on what I can glean from the code you've posted.</p> <p>Summary: Anything of the form <code>while (!whatever.eof())</code> is irretrievably broken. You nearly always want <code>while (read_something())</code> instead. The convention in C++ is to use <code>&gt;&gt;</code> to read an item from a stream. Follow that convention when/if possible. Oh, and yeah, you pretty much always want to pass streams by reference (the exception being the odd time you need to work with a pointer to a stream instead -- unusual, but it does happen now and again).</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.
    1. VO
      singulars
      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