Hello all,
I am trying to make a linked list (class-based). I'm not sure if I went actually did it right. I get a compiler error on the segment of code that deals with printing the linked list to stdout. Here is the seqment in question.
// print contents of list
void Node::printAll() const
{
Node *temp = NULL;
temp = this;
if (temp->previous != NULL)
temp = findFirst(temp);
else
{
do
{
// prints all items except last
// since it tests for next to point to NULL
cout << temp->first << " " << temp->last << endl;
if (temp->next == NULL)
continue;
else
temp = temp->next;
} while (temp->next != NULL);
}
// print last item
cout << temp->first << " " << temp->last << endl;
}
The error my compiler displays is
"cannot convert from const Node *const to Node* Conversion loses qualifiers". I'm not sure how to fix this.
And here is the whole thing for reference.
#ifndef NODE_H
#define NODE_H
#include <string>
using std::string;
class Node
{
public:
Node();
Node* addToFront(Node*);
Node* addToBack(Node*);
Node* findFirst(Node*) const;
Node* findLast(Node*) const;
void printAll() const;
private:
Node *previous, *next;
string first, last;
void collectData(Node*);
};
#endif
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <string>
using std::string;
#include "Node.h"
Node::Node() : previous(NULL), next(NULL)
{
collectData(this);
}
// add new Node to beginning
Node* Node::addToFront(Node *n)
{
Node *temp = new Node;
n = findFirst(n);
temp->next = n;
n->previous = temp;
return temp;
}
// add new Node to end
Node* Node::addToBack(Node *n)
{
Node *temp = new Node;
n = findLast(n);
//point new object to what was last object
temp->previous = n;
// point what was last object to new last object
n->next = temp;
return temp;
}
// returns first item in list
Node* Node::findFirst(Node *n) const
{
// find 1st item in list
if (n->previous != NULL)
{
while (n->previous != NULL)
// point to previous object
n = n->previous;
}
return n;
}
// returns last item in list
Node* Node::findLast(Node *n) const
{
// find last item
if (n->next != NULL)
{
while (n->next != NULL)
// point to next Node object
n = n->next;
}
return n;
}
// print contents of list
void Node::printAll() const
{
Node *temp = NULL;
temp = this;
if (temp->previous != NULL)
temp = findFirst(temp);
else
{
do
{
// prints all items except last
// since it tests for next to point to NULL
cout << temp->first << " " << temp->last << endl;
if (temp->next == NULL)
continue;
else
temp = temp->next;
} while (temp->next != NULL);
}
// print last item
cout << temp->first << " " << temp->last << endl;
}
// helper function; adds data to object
void Node::collectData(Node *n)
{
string first, last;
cout << "Enter first name:" << endl;
cin >> first;
n->first = first;
cout << "Enter last name:" << endl;
cin >> last;
n->last = last;
}
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
#include "Node.h"
int main()
{
Node *me = new Node();
me->addToBack(me);
me->printAll();
me->printAll();
return 0;
}