Note that there are some explanatory texts on larger screens.

plurals
  1. POCompile errors while read/write size of multiple structs to file
    primarykey
    data
    text
    <p>I've already asked 2 questions kind of related to this project, and i've reached this conclusion. Writing the size of the Struct to the file , and then reading it back is the best way to do this.</p> <p>I'm creating a program for a homework assignment that will allow me to maintain inventory. I need to read / write multiple structs of the same type to a file.</p> <p>The problem is... this is really complicated and i'm having trouble wrapping my head around the whole process. I've seen a bunch of examples and i'm trying to put it all together. I'm getting compile errors... and I have zero clue on how to fix them. If you could help me on this I would be so appreciative... thank you. I'm so lost right now... </p> <p>**** HOPEFULLY THE LAST EDIT #3 *************</p> <p>My Code:</p> <pre><code>// Project 5.cpp : main project file. #include "stdafx.h" #include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;string&gt; #include &lt;vector&gt; #include &lt;algorithm&gt; using namespace System; using namespace std; #pragma hdrstop int checkCommand (string line); template&lt;typename Template&gt; void readFromFile(Template&amp;); template&lt;typename Template&gt; void writeToFile(Template&amp;); template&lt;typename T&gt; void writeVector(ofstream &amp;out, const vector&lt;T&gt; &amp;vec); template&lt;typename Template&gt; void readVector(ifstream&amp; in, vector&lt;Template&gt;&amp; vec); struct InventoryItem { string Item; string Description; int Quantity; int wholesaleCost; int retailCost; int dateAdded; } ; int main(void) { cout &lt;&lt; "Welcome to the Inventory Manager extreme! [Version 1.0]" &lt;&lt; endl; vector&lt;InventoryItem&gt; structList; ofstream out("data.dat"); writeVector( out, structList ); while (1) { string line = ""; cout &lt;&lt; endl; cout &lt;&lt; "Commands: " &lt;&lt; endl; cout &lt;&lt; "1: Add a new record " &lt;&lt; endl; cout &lt;&lt; "2: Display a record " &lt;&lt; endl; cout &lt;&lt; "3: Edit a current record " &lt;&lt; endl; cout &lt;&lt; "4: Exit the program " &lt;&lt; endl; cout &lt;&lt; endl; cout &lt;&lt; "Enter a command 1-4: "; getline(cin , line); int rValue = checkCommand(line); if (rValue == 1) { cout &lt;&lt; "You've entered a invalid command! Try Again." &lt;&lt; endl; } else if (rValue == 2){ cout &lt;&lt; "Error calling command!" &lt;&lt; endl; } else if (!rValue) { break; } } system("pause"); return 0; } int checkCommand (string line) { int intReturn = atoi(line.c_str()); int status = 3; switch (intReturn) { case 1: break; case 2: break; case 3: break; case 4: status = 0; break; default: status = 1; break; } return status; } template &lt;typename Template&gt; void readFromFile(Template&amp; t) { ifstream in("data.dat"); readVector(in, t); Need to figure out how to pass the vector structList via a Template in.close(); } template &lt;typename Template&gt; void writeToFile(Template&amp; t) { ofstream out("data.dat"); readVector(out, t); Need to figure out how to pass the vector structList via a Template out.close(); } template&lt;typename T&gt; void writeVector(ofstream &amp;out, const vector&lt;T&gt; &amp;vec) { out &lt;&lt; vec.size(); for(vector&lt;T&gt;::const_iterator i = vec.begin(); i != vec.end(); ++i) { out &lt;&lt; *i; // SUPER long compile error } } template&lt;typename T&gt; vector&lt;T&gt; readVector(ifstream &amp;in) { size_t size; in &gt;&gt; size; vector&lt;T&gt; vec; vec.reserve(size); for(int i = 0; i &lt; size; ++i) { T tmp; in &gt;&gt; tmp; vec.push_back(tmp); } return vec; } </code></pre> <p>My Compile Errors:</p> <pre><code>1&gt;.\Project 5.cpp(128) : error C2679: binary '&lt;&lt;' : no operator found which takes a right-hand operand of type 'const InventoryItem' (or there is no acceptable conversion) 1&gt; C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\ostream(653): could be 'std::basic_ostream&lt;_Elem,_Traits&gt; &amp;std::operator &lt;&lt;&lt;char,std::char_traits&lt;char&gt;&gt;(std::basic_ostream&lt;_Elem,_Traits&gt; &amp;,const char *)' 1&gt; with </code></pre> <p>That is the only error i'm getting now. I see your code is SO Much better. My new compiler error is SUPER long. I've shown where it the error points to. Could you help me just one last time?</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.
 

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