我的代码运行良好,但现在当尝试将其制作成模板时,我遇到了许多看似无关的错误。这是怎么回事?

My code worked perfectly, but now when trying to make it into a template, I'm getting a lot of seemingly unrelated errors. What is going on here?

本文关键字:许多看 遇到 怎么回事 错误 代码 运行 我的      更新时间:2023-10-16

我发现了一个类似于我的问题,但我无法根据他们的解决方案找到解决方案,所以我决定在这里发布我的问题。

我为一个整数链表编写了代码,现在我正试图将其转换为一个模板,这样它就可以是一个任意的链表。当我这样做的时候,我遇到了很多错误,我不知道为什么会因为我的更改而发生这些错误(它们似乎与范围有关,但我没有更改任何范围(,也不知道如何修复它们。

我得到的错误是:

  • "‘LinkedList’的定义或重新声明无法命名全局范围">

  • "使用未声明的标识符'root'">

  • "未知类型名称‘节点’">

  • "'LinkedList'不是类、命名空间或枚举">

下面是我的代码(我会标记我遇到错误的地方(:

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(){}

您还需要将每个实现声明为模板。例如:

LinkedList<DataType>::LinkedList()

应该是:

template<typename DataType>
LinkedList<DataType>::LinkedList()

当然,每个功能的每个实现都需要成为模板。

相关文章: