Note that there are some explanatory texts on larger screens.

plurals
  1. POGarbage Result! C++ LinkedList
    text
    copied!<p>Given the code in main:</p> <pre><code>// main.cpp wineries-&gt;insert(winery("Lopez Island Vinyard", "San Juan Islands", 7, 95)); </code></pre> <p>Two things will happen:</p> <ol> <li><p>The winery constructor is invoked where I have intialized the winery private members:</p> <pre><code>//winery.cpp winery::winery(const char * const name, const char * const location, const int acres, const int rating) : name( new char[strlen(name)+1] ) , location( new char[strlen(location)+1] ) , acres( 0 ), rating( 0 ) { } </code></pre> <p>When it is finished, the result of the <code>this</code> pointer has a garbage value. Why is this? Am I not initializing correctly?</p></li> <li><p>After the winery constructor dies, we go to the <code>list::insert( const winery &amp;winery )</code> function:</p> <pre><code>void list::insert(const winery&amp; winery) { node *NodePtr = new node( winery ); // NodePtr-&gt;item has the garbage. NodePtr-&gt;item = winery; } list::node::node( const winery&amp; winery ) { // This works because I have a default constructor for the winery object // and *only* for that reason... // How can I use the node constructor without having to use a default constructor for the winery class? } </code></pre> <p>Why am I getting garbage as a result of the values passed to the winery constructor?</p> <p>The winery public member functions are as follows, where <code>name</code>, <code>location</code>, <code>acres</code>, and <code>rating</code> are all private members to the winery class.</p> <pre><code>winery::winery() { // do nothing default constructor // only here so I can add the &amp;winery to the node constructor.. } winery::~winery() { delete location; delete name; // your code here } const char * const winery::getName() const { //winery *wine_t = new winery(); const char cName[5] = "four"; // just to see if it still gives garbage.. return cName } const char * const winery::getLocation() const { // return one of winery's private members. // It *will* crash when this function is called. // *That* might be the issue with **garbage values** return location; } </code></pre> <p>Without these functions having parameters, it makes it difficult to transfer the attributes over to a wineryPtr object, and then it would be logical to add the entire winery object to the linkedlist...</p> <pre><code>// list.h #ifndef _LIST_ #define _LIST_ #include &lt;ostream&gt; #include "winery.h" using namespace std; class list { public: list(void); // constructor virtual ~list(void); // destructor ... void insert(const winery&amp; winery); winery * const find(const char * const name) const; private: struct node { node(const winery&amp; winery); // constructor winery item; node * nextByName; node * nextByRating; }; node * headByName; node * headByRating; }; #endif // _LIST_ </code></pre></li> </ol> <p>My questions are a little scattered, and I hope someone out there has the time to help!</p>
 

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