I'm dealing with a doubly Linked List. It's made up of classes and is centered around the current node (instead of a node in the beginning or end of the list). Now my print function will throw an error but only if I have traversed the list at all. My print function just prints data in the current node (providing it's not null). Here's my print function: (much more detailed description of my file hierarchy and code at the bottom)
void queue::print(){
if (current){
std::cout << std::endl << std::endl << "+++++++++++++++++++ Webpage +++++++++++++++++++" << std::endl
<< "URL: " << current->data.getURL() << std::endl
<< "-----------------------------------------------" << std::endl
<< "Title: " << current->data.getTitle() << std::endl
<< "-----------------------------------------------" << std::endl
<< "Content: " << current->data.getContent() << std::endl
<< "+++++++++++++++++++++++++++++++++++++++++++++++" << std::endl << std::endl;
}
else{
std::cout << std::endl << "Your not on a page. Please navigate to a page first." << std::endl;
}
Now, if I've filled the list with two nodes of data, and I execute my print function, it'll print the data in the node just fine. However, if I traverse to the previous node with my goBack() function:
void queue::goBack(){
if (!current->previous){
std::cout << "No previous page to go to!" << std::endl;
}
else{
temp2 = current;
current = current->previous;
}
}
That will execute fine, but when I try to print the data in the node (with the same print function) I receive this error:
Unhandled exception at 0x003E7926 in Web Browser.exe: 0xC0000005: Access violation reading location 0xCDCDCE19.
and visual studio opens up a file without an extension type, with what looks like C code in it, called xstring, which has a break arrow pointing to line 1754 on it.
Now let me explain my code in a little more detail. I have five files: webQueue.h, webQueue.cpp, webPage.h, webPage.cpp, & main.cpp. All of the functions I've listed are in my webQueue.cpp file.
Here's webQueue.h:
#include "webPage.h"
class queue{
public:
queue();
void newPage(std::string u, std::string t, std::string c);
void goForward();
void goBack();
void print();
private:
struct Node{
webPage data;
Node* next;
Node* previous;
};
Node* temp;
Node* current;
Node* temp2;
};
And here's webPage.h:
#include <string>
class webPage{
public:
webPage();
webPage(std::string u, std::string t, std::string c);
std::string getURL();
void setURL(std::string u);
std::string getTitle();
void setTitle(std::string t);
std::string getContent();
void setContent(std::string c);
private:
std::string URL;
std::string title;
std::string content;
};
My webQueue.cpp file includes:
#include <iostream>
#include "webQueue.h"
My webPage.cpp file just includes webPage.h and my main.cpp file (which contains my executing function) includes:
#include <iostream>
#include <string>
#include "webQueue.h"
Although these relations may seem a little convoluted, they should all be valid. The cpp files are linked to their header files with the exact same name (providing the header file exists), along with main.cpp being linked to webQueue.h and webQueue.h being linked to webPage.h. I can't see what I'm doing wrong with my code—although that's probably just because I'm having such a hard time understanding exactly how pointers work. I imagine that the error is in the code of my print(), goBack(), and goForward() functions (although I can't test my goForward() funtion until I fix my goBack() function) but I can't tell what's wrong.
Any help you guys can give would be greatly appreciated, as I am stumped. Here's a dropbox link to all the files so that you can test this program for yourself and see if I have any other functions with errors: https://www.dropbox.com/s/yekrz6dln1v9npk/webQueue.zip?dl=0
Your
Node
management is wrong.One minor issue is that
queue::goBack()
andqueue::goForward()
are not checking ifcurrent
is null before accessing its fields, so your code will crash if the user chooses to "go back" or "go forward" before choosing to "go to webpage" (at leastprint()
is checking for null). So add those checks, and maybe even updateprintMenu()
to not even output those options when the queue does not have a current page available.But more importantly, your
queue::newPage()
implementation is completely broken. Whencurrent
is not null, you are setting that node'snext
member to point at itself instead of the new node you have created, and you are not setting the new node'sprevious
field at all, let alone to point at the existing node that it is being inserted after.You should also get rid of the
temp
andtemp2
members of thequeue
class. They do not belong there in the first place. They are only useful inside ofqueue::newPage()
(queue::goBack()
does not need to usetemp
at all, just likegoForward()
), so they should be changed into local variables inside ofqueue::newPage()
only. Even better, they can just be removed altogether, asqueue::newPage()
can be implemented without using them at all.Your
queue
implementation should look more like this instead (and this is not even counting copy/move semantics, either - see rule of three/five/zero):And then, while you are at it, you should rewrite
queue
to stop using manual node management altogether and use the standardstd::list
class instead: