[英]Why am I getting declaration with no type error - linked list?
Hi I am trying to implement a linked list using templates and ADT. 嗨,我正在尝试使用模板和ADT实现链接列表。 At the moment I have two classes.
目前我有两节课。 One is an iterator for linked list and the other is the base class for linked lists that I will use to derive linked list classes from.
一个是链表的迭代器,另一个是链表的基类,我将用它来从中派生链表。
When trying to implement two functions that will give me an iterator at the start and end of the list respectivly I get compile error saying "ISO C++ forbids declaration of 'linkedListIterator' with no type" 当试图实现两个函数时,我会在列表的开头和结尾给我一个迭代器,我得到编译错误,说“ISO C ++禁止声明'linkedListIterator'没有类型”
Here is the code for the definition of the iterator: 这是迭代器定义的代码:
#ifndef LINKEDLISTITERATOR_H
#define LINKEDLISTITERATOR_H
#include <stddef.h> //for NULL
#include "nodetype.h"
#include "linkedlisttype.h"
template <class Type>
class linkedListIterator
{
public:
linkedListIterator();
linkedListIterator(nodeType<Type> *ptr);
Type operator*();
linkedListIterator<Type> operator++();
bool operator==(const linkedListIterator<Type>& right) const;
bool operator!=(const linkedListIterator<Type>& right) const;
private:
nodeType<Type> *current;
};
#endif // LINKEDLISTITERATOR_H
Here is the code for the definition of the node Type 以下是节点类型定义的代码
#ifndef NODETYPE_H_INCLUDED
#define NODETYPE_H_INCLUDED
//Definition of the node
template <class Type>
struct nodeType
{
Type info;
nodeType<Type> *link;
};
#endif // NODETYPE_H_INCLUDED
Here is the definition of the linkedlist base class: 以下是linkedlist基类的定义:
#ifndef LINKEDLISTTYPE_H
#define LINKEDLISTTYPE_H
#include "nodetype.h"
#include "linkedlistiterator.h"
//Definition of linked list
template <class Type>
class linkedListType
{
public:
const linkedListType<Type>& operator=
(const linkedListType<Type>&);
void initializeList();
bool isEmptyList() const;
void print() const;
int length() const;
void destroyList();
Type front() const;
Type back() const;
virtual bool search(const Type& searchItem) const = 0;
virtual void insertFirst(const Type& newItem) = 0;
virtual void insertLast(const Type& newItem) = 0;
virtual void deleteNode(const Type& deleteItem) = 0;
// this is where the error comes
linkedListIterator<Type> begin();
// and here as well
linkedListIterator<Type> end();
linkedListType();
linkedListType(const linkedListType<Type>& otherList);
~linkedListType();
protected:
int count;
nodeType<Type> *first;
nodeType<Type> *last;
private:
void copyList(const linkedListType<Type>& otherList);
};
#endif // LINKEDLISTTYPE_H
I am new to templates and ADT so trying to wrap my mind around this. 我是模板和ADT的新手,所以试着把我的思想包围起来。 Any help will be most appreciated please.
任何帮助将非常感谢。
You have two headers which each try to include each other. 你有两个标题,每个标题都试图互相包含。 The result is that, if you
#include "linkedlistiterator.h"
, the definition of linkedListType
appears before that of linkedListIterator
; 其结果是,如果你
#include "linkedlistiterator.h"
的定义中, linkedListType
之前的出现linkedListIterator
; hence the error due to linkedListIterator
not being declared at that point. 因此,由于在此时未声明
linkedListIterator
而导致的错误。
In this case, it looks like the iterator type does not depend on the list type at all, so you can simply remove the #include "linkedlistlype.h"
from "linkedlistiterator.h"
. 在这种情况下,看起来迭代器类型根本不依赖于列表类型,因此您只需从
"linkedlistiterator.h"
删除#include "linkedlistlype.h"
"linkedlistiterator.h"
。
Seems that both linkedlisttype.h
and linkedlistiterator.h
include each other. 似乎
linkedlisttype.h
和linkedlistiterator.h
都linkedlisttype.h
包含。
That indicates rather close coupling in your mind. 这表明你心中的联系相当紧密。 You probably want to have
LinkedList<T>
class and nested LinkedList<T>::Iterator
class. 您可能希望使用
LinkedList<T>
类和嵌套的LinkedList<T>::Iterator
类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.