C++ 我的代码工作得很好,但现在当我试图将其制作成模板时,我';我收到了很多看似无关的错误。这是怎么回事?
我发现了一个与我类似的问题,但我无法根据他们的解决方案找出解决方案,所以我决定将我的问题发布在这里 我为一个整数链表编写了代码,现在我正试图将它转换成一个模板,这样它就可以成为任何东西的链表。当我这样做的时候,我得到了大量的错误,我无法弄清楚它们为什么会发生,因为我的更改(它们似乎与范围相关,但我没有更改任何范围)或者如何修复它们 我得到的错误是:C++ 我的代码工作得很好,但现在当我试图将其制作成模板时,我';我收到了很多看似无关的错误。这是怎么回事?,c++,c++11,c++14,C++,C++11,C++14,我发现了一个与我类似的问题,但我无法根据他们的解决方案找出解决方案,所以我决定将我的问题发布在这里 我为一个整数链表编写了代码,现在我正试图将它转换成一个模板,这样它就可以成为任何东西的链表。当我这样做的时候,我得到了大量的错误,我无法弄清楚它们为什么会发生,因为我的更改(它们似乎与范围相关,但我没有更改任何范围)或者如何修复它们 我得到的错误是: “LinkedList的定义或重新声明无法命名全局作用域” “使用未声明的标识符'root'” “未知类型名称‘节点’” “LinkedList”
- “LinkedList的定义或重新声明无法命名全局作用域”
- “使用未声明的标识符'root'”
- “未知类型名称‘节点’”
- “LinkedList”不是类、命名空间或枚举
模板
类链接列表{
公众:
结构节点{
//Node(Node*aNext=nullptr):下一个(aNext){}
数据类型值;//这是要保存的值
Node*next;//指向列表中的下一个节点(或nullptr)
};
//friend类迭代器;//即使是嵌套类,也要执行此操作
节点*根;
//---------------------------------------------------------------
//添加嵌套迭代器类。。。
类迭代器{
公众:
迭代器();//默认构造函数
//迭代器():当前(nullptr){}
迭代器(节点*阳极);
//迭代器(节点*阳极):当前(阳极){};//构造函数
迭代器和运算符=(const LinkedList::Iterator&aCopy)noexcept;//添加了这个删除伪对象吗?
~Iterator();//dtor
迭代器运算符++();
迭代器运算符++(数据类型);
布尔运算符==(常量迭代器和反迭代器);
布尔运算符!=(常量迭代器和反迭代器);
数据类型运算符*();
操作员节点*();
Node*current;//由于LinkedList是嵌套类,是否需要放置它?
//添加所有必要的操作符
受保护的:
};
//--------------------------------------------------------------
LinkedList();//默认构造函数。。。
LinkedList(const LinkedList&aCopy);//复制
~LinkedList();//dtor
LinkedList&operator=(常量LinkedList&aCopy);//赋值运算符
void append(数据类型值);
void prepend(数据类型值);
作废删除(数据类型值);
int size();//需要返回未签名的int????
迭代器begin();
迭代器end();
迭代器查找(数据类型aValue);//查找
受保护的:
};
//类迭代器;
LinkedList::LinkedList(){//ERROR:“LinkedList”的定义或重新声明无法命名全局作用域”
root=nullptr;//错误:使用了未声明的标识符“root”
}
LinkedList::LinkedList(const LinkedList&aCopy){//copy ctor//ERROR:“LinkedList”的定义或重新声明无法命名全局作用域
Node*temp=aCopy.root;//错误:未知类型名称“Node”
Node*newNode=newNode;//错误:未知类型名称“Node”
root=newNode;//错误:使用了未声明的标识符“root”
while(temp!=nullptr){
新建节点->值=临时->值;
温度=温度->下一步;
如果(温度!=nullptr){
新建节点->下一步=新建节点;
newNode=newNode->next;
}
else{newNode->next=nullptr;}
}
}
LinkedList&LinkedList::operator=(const LinkedList&aCopy){//赋值运算符
while(root!=nullptr){
Node*oneBefore=root;
根=根->下一步;
删除之前的一个;
}
Node*newNode=新节点;
Node*temp=aCopy.root;
根=新节点;
while(temp!=nullptr){
新建节点->值=临时->值;
温度=温度->下一步;
如果(温度!=nullptr){
新建节点->下一步=新建节点;
newNode=newNode->next;
}
else{newNode->next=nullptr;}
}
归还*这个;
}
LinkedList::~LinkedList(){//dtor
节点*oneBefore=nullptr;
while(root!=nullptr){
oneBefore=根;
根=根->下一步;
删除之前的一个;
}
}
LinkedList::迭代器LinkedList::find(数据类型aValue){
节点*temp=root;
迭代器myIterator=begin();
对于(myIterator=this->begin();myIterator!=this->end();myIterator++){
如果(临时->值==aValue){
返回温度;
}
温度=温度->下一步;
}
返回空ptr;
}
void LinkedList::append(数据类型值){
Node*newNode=新节点;
新建节点->值=值;
if(root!=nullptr){
节点*temp=root;
while(临时->下一步!=nullptr){
温度=温度->下一步;
}
newNode->next=nullptr;
temp->next=newNode;
}
if(root==nullptr){
newNode->next=nullptr;
根=新节点;
}
}
void LinkedList::prepend(数据类型值){//错误:“LinkedList”不是类、命名空间或枚举
Node*newNode=新节点;
新建节点->值=值;
if(root!=nullptr){
newNode->next=root;
根=新节点;
}
if(root==nullptr){
根=新节点;
newNode->next=nullptr;
}
}
void LinkedList::remove(数据类型值){//错误:“LinkedList”不是类、命名空间或枚举
if(root!=nullptr){
节点*before=nullptr;
节点*temp=root;
如果(温度->值==值){
root=temp->next;
}
否则{
while(temp->value!=value&&temp->next!=nullptr){
前=温度;
温度=温度->下一步;
}
如果(温度->值==值){
前->下一步=临时->下一步;
}
}
删除临时文件;
}
}
int LinkedList::size(){//错误:“LinkedList”不是类、命名空间或枚举
节点*阳极=根;
整数=0;
while(阳极!=nullptr){
阳极=阳极->下一步;
数值=数值+1;
}
回礼;
}
LinkedList::迭代器LinkedList::b
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()