Hello there, long time lurker, first time poster and was wondering if someone could give me a hand with this small pointer issue.
In the following code I'm implementing a double linked list, I've posted the code which I think is most relevant.
What happens is the debugger tries to check if the pointer *first points to a null value and if so to give it the value of the passed in parameter. This makes sense to me but when I run the code I get an access violation.
Any help appreciated, this really feels like something simple but I can't quite put my finger on it.
template <class T>
class DoubleLinkedList {
private:
typedef DoubleLinkedNode<T> Node;
//The first node in the list
Node* first;
//The last node in the list
Node* last;
//The length of the list
int _length;
public:
DoubleLinkedList() : first(0), last(0), _length(0){};
~DoubleLinkedList(){
while(first->getNext() != (Node*)0){
first->getNext()->dropNode();
}
};
void append(const T &newelement){
Node *N = new Node(newelement);
if(first == (Node*)0) //Problem happens here
{
first = N;
last = N;
_length = 1;
}else
{
last->insertNodeAfter(N);
last = N;
_length++;
}
};
};
int main(int argc, char* argv[]){
string s1("One");
DoubleLinkedList<string>* L = 0;
L->append(s1);
}
cire thank you so much. I've been trying to bunker down on my pointer use but turns out I began using them a bit too much and replacing variables with pointers!