Note that there are some explanatory texts on larger screens.

plurals
  1. POUser defined linked list returning pointer from function
    text
    copied!<p>I know i asked a similar question a while back but its still not working. Sorry for repetition i am just not sure why it does not work. I created my own linked list(not using the stl library) I have my classes set up and work perfectly how I want them too. The problem I have is getting a reference back when I use a function to populate the list. in my main I call the function <code>bookSetUp()</code> which is used to populate the list. I make the this function equal to a temp pointer. (Heres the code)<br> <strong>Main.cpp</strong></p> <pre><code>Book* temp = bookSetUp(); while(temp!=NULL) { cout&lt;&lt;temp-&gt;getName()&lt;&lt;endl; cout&lt;&lt;temp-&gt;getAuthor()&lt;&lt;endl; cout&lt;&lt;temp-&gt;getISBN()&lt;&lt;endl; temp = temp-&gt;getNext(); } </code></pre> <p>bookSetUp() is located in another .cpp file called <strong>functions.cpp</strong>(Links are working between the two .cpp's as i have tested them)<br> <strong>functions.cpp</strong><br> <code>Book* bookSetUp()<br> {</code></p> <pre><code>//The items that populate the list Book a("A Tale of Two Cities", "Charles Dickens", 1203456, true); Book b("Lord of the rings", "J.R.R Tolkein", 123456, true); Book c("Le Petit Prince", "Antoine de Saint-Exupéry", 123457, true); Book d("And Then There Were None", "Agatha Christie", 123458, true); Book e("Dream of the Red Chamber","Cao Xueqin",123459, true); Book f("The Hobbit","J.R.R Tolkein",123467, true); a.setPrev(NULL); a.setNext(&amp;b); b.setPrev(&amp;a); b.setNext(&amp;c); c.setPrev(&amp;b); c.setNext(&amp;d); d.setPrev(&amp;c); d.setNext(&amp;e); e.setPrev(&amp;d); e.setNext(&amp;f); f.setPrev(&amp;e); f.setNext(NULL); Book* temp = &amp;a; return temp; } </code></pre> <p>As you can see i am trying to return a pointer to the first item on the list so that main has access to it. My book class has attributes: string name,string author,int isbn and bool availability.<br> The program crashes with <strong>"Unhandled exception at 0x00B16886 in bookRepository.exe: 0xC0000005: Access violation reading location 0xCCCCCD04."</strong> which im guessing means it is not getting a value back from the bookSetUp() function.<br> Also in the debugger the int ISBN returns but not the string(s) or bool.<br> Does anyone know why this is not working? it would be a massive help if you could!</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