我的代码运行良好,但现在当尝试将其制作成模板时,我遇到了许多看似无关的错误。这是怎么回事?
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?
我发现了一个类似于我的问题,但我无法根据他们的解决方案找到解决方案,所以我决定在这里发布我的问题。
我为一个整数链表编写了代码,现在我正试图将其转换为一个模板,这样它就可以是一个任意的链表。当我这样做的时候,我遇到了很多错误,我不知道为什么会因为我的更改而发生这些错误(它们似乎与范围有关,但我没有更改任何范围(,也不知道如何修复它们。
我得到的错误是:
-
"‘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()
当然,每个功能的每个实现都需要成为模板。
相关文章:
- 编译包含字符串的代码时遇到问题
- std::vector的包装器,使数组的结构看起来像结构的数组
- 遇到新行时,有没有办法停止istream_iterator
- 在混合代码库中将C转换为C++时出现许多包含错误
- 看起来is_nothrow_constructible_v()在MSVC中被破坏了,我错了吗
- 从函数角度看ID到文件路径的内部与外部映射
- 在使用GPU支持编译Tensorflow时,会遇到CUDA_TOOLKIT_PATH未绑定变量
- 我不断收到 [错误] ID 返回 1 退出状态错误,但看不到问题所在
- 如何在C++向量中奇数元素前面加上值-1,我在使用insert函数时遇到了问题
- 在 for 循环中查找问题时遇到困难
- 创建结构的数组时遇到分段错误
- 代码在我的计算机上运行良好,但是在将其提交给coursera时遇到未知的信号11问题
- 学习多线程C++:添加线程不会使执行速度更快,即使它看起来应该
- 为什么我遇到分段错误?
- 在顶点着色器中使用 OpenGl 的未声明标识符,我在顶点着色器中绘制三角形时遇到问题
- 这些是什么样的错误?即使我不在 Linux 上工作,我也遇到了 Linux 错误
- 函数在许多数字上转换为基数 1 时减去 2?
- 我的代码运行良好,但现在当尝试将其制作成模板时,我遇到了许多看似无关的错误。这是怎么回事?
- 我正在学习编程,我遇到了看和说序列.我尝试用c ++编写代码
- 使用g++4.6.1,我在尝试包含xaudio2.h时遇到了许多C++编译错误