I found a question similar to mine, but I was not able to figure out a solution based on their solution, so I decided to post mine here.
I wrote code for a linked list of integers, and I am now trying to convert it into a template so it can be a linked list of whatever. When I did so, I got a ton of errors that I can't figure out why they happened due to my changes (they seem related to scope, but I didn't change any scoping) or how to fix them.
The errors I am getting are:
"Definition or redeclaration of 'LinkedList' cannot name the global scope"
"Use of undeclared identifier 'root'"
"Unknown type name 'Node'"
"'LinkedList' is not a class, namespace, or enumeration"
Below is my code (I'll mark where I am getting errors):
template <typename DataType>
class LinkedList {
public:
struct Node {
//Node(Node *aNext=nullptr) : next(aNext) {}
DataType value; //this is the value you want to save
Node *next; //this points to the next node in the list (or nullptr)
};
//friend class Iterator; //do Ineed this even though it's a nested class
Node *root;
//---------------------------------------------------------------
//add a NESTED Iterator class...
class Iterator {
public:
Iterator();//default constructor
//Iterator() : current(nullptr) {}
Iterator(Node* aNode);
//Iterator(Node* aNode): current(aNode){};//constructor
Iterator& operator=(const LinkedList::Iterator& aCopy) noexcept; //added this DELETE DUMMY?
~Iterator(); //dtor
Iterator operator++();
Iterator operator++(DataType);
bool operator==(const Iterator &anIterator);
bool operator!=(const Iterator &anIterator);
DataType operator*();
operator Node*();
Node *current; //do I need to put LinkedList since it's a nested class?
//add all the necessary operators
protected:
};
//--------------------------------------------------------------
LinkedList(); //default constructor...
LinkedList(const LinkedList& aCopy); //copy ctor
~LinkedList(); //dtor
LinkedList& operator=(const LinkedList& aCopy); //assignment operator
void append(DataType value);
void prepend(DataType value);
void remove(DataType value);
int size(); //needs to return unsigned int????
Iterator begin();
Iterator end();
Iterator find(DataType aValue); //find
protected:
};
//class Iterator;
LinkedList<DataType>::LinkedList() { //ERROR: "Definition or redeclaration of 'LinkedList' cannot name the global scope"
root=nullptr; //ERROR: Use of undeclared identifier 'root'
}
LinkedList<DataType>::LinkedList(const LinkedList& aCopy){ //copy ctor //ERROR: Definition or redeclaration of 'LinkedList' cannot name the global scope
Node *temp=aCopy.root; //ERROR: Unknown type name 'Node'
Node *newNode = new Node; //ERROR: Unknown type name 'Node'
root=newNode; //ERROR: Use of undeclared identifier 'root'
while (temp != nullptr){
newNode-> value=temp->value;
temp=temp->next;
if (temp !=nullptr){
newNode->next=new Node;
newNode=newNode->next;
}
else{ newNode->next=nullptr;}
}
}
LinkedList& LinkedList::operator=(const LinkedList &aCopy){ //assignment operator
while(root!=nullptr){
Node* oneBefore= root;
root =root->next;
delete oneBefore;
}
Node *newNode= new Node;
Node *temp=aCopy.root;
root=newNode;
while(temp!=nullptr){
newNode->value=temp->value;
temp=temp->next;
if(temp!=nullptr){
newNode->next=new Node;
newNode=newNode->next;
}
else{newNode->next=nullptr;}
}
return *this;
}
LinkedList::~LinkedList(){ //dtor
Node* oneBefore = nullptr;
while(root!=nullptr){
oneBefore=root;
root=root->next;
delete oneBefore;
}
}
LinkedList::Iterator LinkedList::find(DataType aValue){
Node* temp=root;
Iterator myIterator = begin();
for(myIterator = this->begin(); myIterator != this->end(); myIterator++){
if(temp->value==aValue){
return temp;
}
temp=temp->next;
}
return nullptr;
}
void LinkedList::append(DataType value){
Node* newNode=new Node;
newNode->value=value;
if(root!=nullptr){
Node* temp = root;
while (temp->next !=nullptr){
temp=temp->next;
}
newNode->next=nullptr;
temp->next=newNode;
}
if(root==nullptr){
newNode->next=nullptr;
root=newNode;
}
}
void LinkedList::prepend(DataType value){ //ERROR: 'LinkedList' is not a class, namespace, or enumeration
Node* newNode=new Node;
newNode->value=value;
if (root!=nullptr){
newNode->next=root;
root=newNode;
}
if(root==nullptr){
root=newNode;
newNode->next=nullptr;
}
}
void LinkedList::remove(DataType value){ //ERROR: 'LinkedList' is not a class, namespace, or enumeration
if(root!=nullptr){
Node *before=nullptr;
Node *temp=root;
if(temp->value==value){
root=temp->next;
}
else{
while(temp->value!=value &&temp->next != nullptr){
before=temp;
temp=temp->next;
}
if(temp->value==value){
before->next=temp->next;
}
}
delete temp;
}
}
int LinkedList::size(){ //ERROR: 'LinkedList' is not a class, namespace, or enumeration
Node* aNode = root;
int numElements=0;
while(aNode!=nullptr){
aNode=aNode->next;
numElements=numElements+1;
}
return numElements;
}
LinkedList::Iterator LinkedList::begin(){ //ERROR:'LinkedList' is not a class, namespace, or enumeration
return LinkedList::Iterator(root);
}
LinkedList::Iterator LinkedList::end(){ //ERROR:'LinkedList' is not a class, namespace, or enumeration
Node *aNode=root;
while(aNode!=nullptr){
aNode=aNode->next;
}
return LinkedList::Iterator(aNode);
}
LinkedList::Iterator::Iterator() : current(nullptr) {} //ERROR: 'LinkedList' is not a class, namespace, or enumeration
LinkedList::Iterator::Iterator(Node* aNode): current(aNode){ //ERROR: 'LinkedList' is not a class, namespace, or enumeration
};
LinkedList::Iterator LinkedList::Iterator::operator++(){//I have no idea what the difference is supposed to be between this one and the one below
current=current->next;
return *this;
}
LinkedList::Iterator LinkedList::Iterator::operator++(DataType){//I have no idea what the difference is supposed to be between this one and the one below
current=current->next;
return *this;
}
LinkedList::Iterator& LinkedList::Iterator::operator=(const LinkedList::Iterator& aCopy) noexcept{ //assignment operator
current=aCopy.current;
return *this;
}
bool LinkedList::Iterator::operator !=(const LinkedList::Iterator& aCopy){
return current != aCopy.current;
}
bool LinkedList::Iterator::operator==(const LinkedList::Iterator& aCopy){
return current==aCopy.current;
}
DataType LinkedList::Iterator::operator*(){
return current->value;
}
LinkedList::Iterator::~Iterator(){}
You need to declare each of your implementations to be templates as well. For instance:
should be:
Of course, every implementation of each function will need to become templates as well.