Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Templates and Classes
    text
    copied!<p>I would like to sort different types of numbers in different files as a learning exercise. I've managed to get doubles and ints to sort correctly, but cannot get my own type (to sort a deck of cards) to format correctly. I've managed to get it narrowed down to my <strong>sort function in the Reader Class</strong>. I'm getting all sorts of errors. What exactly am I doing wrong? Or am I missing something? Thanks everyone, I hope I explained my problems clearly.</p> <p>My file I created for Cards looks something like... </p> <pre><code>1 11 (meaning Jack of Diamonds) 2 8 (meaning 8 of hearts) </code></pre> <p>etc...</p> <p>errors: </p> <blockquote> <p>error: no match for 'operator&lt;' in '((Reader<em>)this)->Reader::array[compareIndex] &lt; ((Reader</em>)this)->Reader::array[smallestIndex]'</p> <p>error: cannot convert 'Card' to 'int' in initialization</p> <p>error: no match for 'operator=' in '((Reader*)this)->Reader::array[smallestIndex] = temp'</p> </blockquote> <p><strong>My Code</strong></p> <p>Lastly, Main</p> <pre><code>#include &lt;iostream&gt; #include "Reader.h" #include "Card.h" using namespace std; int main() { Reader&lt;int&gt; nums; Reader&lt;double&gt; dubs; Reader&lt;Card&gt; cards; int number; do { cout &lt;&lt; "Enter 1 if your file contains integers\n" &lt;&lt; "Enter 2 if your file contains doubles\n" &lt;&lt; "Enter 3 if your file contains Cards:\n" &lt;&lt; "Enter a number: "; cin &gt;&gt; number; if (number == 1){ nums.open_IO(); nums.read_array(); nums.sort(); nums.write_array(); nums.close_IO(); } else if (number == 2){ dubs.open_IO(); dubs.read_array(); dubs.sort(); dubs.write_array(); dubs.close_IO(); } else if (number == 3){ cards.open_IO(); cards.read_array(); cards.sort(); cards.write_array(); cards.close_IO(); } } while ((number != 1) &amp;&amp; (number != 2) &amp;&amp; (number != 3)); } </code></pre> <p>Reader.h</p> <pre><code>#ifndef READER_H #define READER_H #include &lt;string&gt; #include &lt;fstream&gt; using namespace std; template &lt;class rList&gt; class Reader { public: static const int SIZE = 50; Reader(); bool open_IO(); void close_IO(); bool read_array(); bool write_array(); void sort(); private: // Ask for the files void ask_filenames(); rList array[SIZE]; int readSize; // input file names and stream to get the data string readFileName; ifstream inFile; // output file names and stream to get the data string writeFileName; ofstream outFile; bool askedFileNames; }; #endif #include "Reader.cpp" </code></pre> <p>My Reader.Cpp file</p> <pre><code>// File: Reader.cpp #ifndef READER_CPP #define READER_CPP #include "Reader.h" #include &lt;iostream&gt; using namespace std; /*************************************************************************** * Contructors and modifying functions defined in "Reader.h" ***************************************************************************/ template &lt;class rList&gt; Reader&lt;rList&gt;::Reader() { askedFileNames = false; readFileName = ""; writeFileName = ""; readSize = 0; rList empty; for( int i = 0; i&lt;SIZE; i++ ) array[i]=empty; } template &lt;class rList&gt; bool Reader&lt;rList&gt;::open_IO(){ if(!askedFileNames) ask_filenames(); inFile.open(readFileName.c_str()); //we can't pass a string, we need a char array if(!inFile.is_open()) { return false; } outFile.open(writeFileName.c_str()); if(!outFile.is_open()) { inFile.close(); //inFile opened successfully so it needs to be closed now return false; } return true; } template &lt;class rList&gt; void Reader&lt;rList&gt;::close_IO() { inFile.close(); outFile.close(); } template &lt;class rList&gt; bool Reader&lt;rList&gt;::read_array() { if(inFile.is_open()) { inFile &gt;&gt; readSize; int index = 0; while(!inFile.eof() &amp;&amp; index &lt; readSize) { inFile &gt;&gt; array[index++]; //increments index after assigning value } readSize = index; //the input file could have had fewer numbers so set readSize return true; } return false; } template &lt;class rList&gt; bool Reader&lt;rList&gt;::write_array() { if(outFile.is_open()) { outFile &lt;&lt; readSize &lt;&lt; endl; //don't forget the number indicating the element co int index = 0; while(index &lt; readSize) { outFile &lt;&lt; array[index++] &lt;&lt; endl; } return true; } return false; } template &lt;class rList&gt; void Reader&lt;rList&gt;::sort() { int startIndex = 0; int compareIndex; int smallestIndex; bool smallerFound; while(startIndex &lt; readSize) { smallestIndex = startIndex; compareIndex = startIndex + 1; smallerFound = false; while(compareIndex &lt; readSize) { //find the smallest value from the starting index if(array[compareIndex] &lt; array[smallestIndex]) { smallestIndex = compareIndex; smallerFound = true; } compareIndex++; } if(smallerFound) { //only swap the values if a smaller value is found int temp = array[startIndex]; array[startIndex] = array[smallestIndex]; array[smallestIndex] = temp; } startIndex++; } } /*-------------------------------------------------------------- This function asks the user for the filenames. This operation is placed in a separate function because it is called multiple times. --------------------------------------------------------------*/ template &lt;class rList&gt; void Reader&lt;rList&gt;::ask_filenames() { cout &lt;&lt; "Welcome. Please type in the name of the file to read the numbers.\n"; cin &gt;&gt; readFileName; cout &lt;&lt; "Thank you. Please type in the name of the file to write the numbers.\n"; cin &gt;&gt; writeFileName; askedFileNames = true; } #endif </code></pre> <p>Card.h</p> <pre><code>#include &lt;ostream&gt; #include &lt;string&gt; #include "Reader.h" using namespace std; class Card{ public: static const int SIZE = 50; enum SUIT {clubs, diams, hears, spads }; enum RANK {ace=1, two, thr, fou, fiv, six, sev, eig, nin, ten, jac, que, kin}; Card(); Card(int newSuit, int newRank); void change(int newSuit, int newRank); friend ostream&amp; operator &lt;&lt; ( ostream&amp; out, Card theCard ); friend istream&amp; operator &gt;&gt; ( istream&amp; in, Card theCard ); private: void change_rank(int newRank); void change_suit(int newSuit); string get_rank() const; string get_suit() const; SUIT suit; RANK rank; }; </code></pre> <p>Card.cpp</p> <pre><code> #include &lt;iostream&gt; #include &lt;string&gt; #include "Card.h" using namespace std; Card::Card() { suit = spads; rank = ace; } Card::Card(int newSuit, int newRank) { change(newSuit, newRank); } void Card::change( int newSuit, int newRank ) { change_suit( newSuit ); change_rank( newRank ); } ostream&amp; operator &lt;&lt; ( ostream&amp; out, Card theCard ) { out &lt;&lt; theCard.get_rank() &lt;&lt; " of " &lt;&lt; theCard.get_suit(); return out; } istream&amp; operator &gt;&gt; ( istream&amp; in, Card theCard ) { int aSuit; int aRank; in &gt;&gt; aSuit &gt;&gt; aRank; return in; } // private member functions to set the private variables with their // corresponding values: integer input -&gt; enumerated type; void Card::change_rank( int newRank ) { if( newRank == ace ) rank = ace; else if( newRank == two ) rank = two; else if( newRank == thr ) rank = thr; else if( newRank == fou ) rank = fou; else if( newRank == fiv ) rank = fiv; else if( newRank == six ) rank = six; else if( newRank == sev ) rank = sev; else if( newRank == eig ) rank = eig; else if( newRank == nin ) rank = nin; else if( newRank == ten ) rank = ten; else if( newRank == jac ) rank = jac; else if( newRank == que ) rank = que; else if( newRank == kin ) rank = kin; } void Card::change_suit( int newSuit ) { if( newSuit == clubs ) suit = clubs; else if( newSuit == spads ) suit = spads; else if( newSuit == diams ) suit = diams; else if( newSuit == hears ) suit = hears; } // Private member functions to extract the information from the card // class. string Card::get_rank() const { if( rank == ace ) return "ace"; if( rank == two ) return "two"; if( rank == thr ) return "three"; if( rank == fou ) return "four"; if( rank == fiv ) return "five"; if( rank == six ) return "six"; if( rank == sev ) return "seven"; if( rank == eig ) return "eight"; if( rank == nin ) return "nine"; if( rank == ten ) return "ten"; if( rank == jac ) return "jack"; if( rank == que ) return "queen"; if( rank == kin ) return "king"; return "get_rank: error"; } string Card::get_suit() const { if( suit == diams ) return "D"; if( suit == hears ) return "H"; if( suit == spads ) return "S"; if( suit == clubs ) return "C"; return "get_suit: error"; } </code></pre>
 

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