简体   繁体   English

为什么我得到没有类型错误的声明 - 链表?

[英]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.hlinkedlistiterator.hlinkedlisttype.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.

相关问题 为什么在前向声明后出现未定义的类型错误? - Why am I getting undefined type error after a forward-declaration? 我正在尝试在链接列表的后面添加内容,但是我不断收到细分错误,有人可以解释原因吗? - I am trying to add at the back of the linked list but i keep getting segmentation error, can someone explain why? 为什么我在声明本身中收到“未在范围内声明”错误? - Why am I getting a "not declared in scope" error in the declaration itself? 为什么在实现链表时出现分段错误? - Why am I getting segmentation fault while implementing linked list? 为什么我会为链表获得额外的 output - Why am I getting extra output for linked list 为什么我得到“声明与(x)不兼容”? - Why am I getting a “declaration is incompatible with (x)”? 为什么我在 `./a.out&#39; 中得到 *** 错误:双链表损坏:0x0000000001c8a000 *** 来自插入函数? - Why am I getting *** Error in `./a.out': corrupted double-linked list: 0x0000000001c8a000 *** from insert function? 我没有得到链表输出 - I am not getting linked list ouput 当我 go 打印我的链接列表时,为什么会出现读取访问冲突? - Why am I getting a read access violation when I go to print my linked list? 为什么在标头中的类声明中声明变量时出现错误? - Why am I getting an error when declaring a variable in a class declaration within a header?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM