Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>NOTE: This answer is just addressing some of the things you are doing incorrectly. This is not the best way to do what you are doing. An array of <code>stock**</code> would make more sense.</p> </blockquote> <p>Doesn't your stock class have a constructor? You don't need to know anything about the stock class if it is a proper object:</p> <pre><code>hash::hash(int capacity) // change this to unsigned and then you can't have capacity &lt; 0 : isAdded(0) , hashTable(0) // don't call new here with bad parameters { if ( capacity &lt; 1 ) exit(-1); // this should throw something, maybe bad_alloc maxSize = capacity; hashTable = new stock[capacity]; // this calls the stock() constructor // constructor already called. All this code is useless // We can initialize our attributes for the stock // to NULL, and test for that when searching. // for ( int index = 0; index &lt; maxSize; index++ ) // { // hashTable[index].name = NULL; // hashTable[index].sharePrice = NULL; // hashTable[index].symbol = NULL; // } } class stock { char* name; // these should be std::string as it will save you many headaches char* sharePrice; // but I'll do it your way here so you can see how to char* symbol; // avoid memory leaks public: stock() : name(0), sharePrice(0), symbol(0) {} ~stock() { delete[] name; delete[] sharePrice; delete[] symbol; } setName(const char* n) { name = new char[strlen(n)+1]; strcpy(name, n); } setPrice(const char* p) { sharePrice = new char[strlen(p)+1]; strcpy(sharePrice, p); } setSymbol(const char* s) { symbol = new char[strlen(s)+1]; strcpy(symbol, n); } const char* getName() const { return name; } const char* getPrice() const { return sharePrice; } const char* getSymbol() const { return symbol; } } </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