简体   繁体   English

错误未命名类型

[英]error does not name a type

I'm getting the compiler error: error "Node" does not name a type. 我收到编译器错误:错误“节点”未命名类型。 This is my header: 这是我的标题:

#ifndef LINKEDLIST_H
#define LINKEDLIST_H



template <class ItemType>
class LinkedList
{
public:
    LinkedList(); // Constructor

    bool isEmpty() const; // Checks if list is empty.
    int getLength() const; // Returns the amount of times in the list.
    bool insert(int index, const ItemType& insertItem); // Inserts an item at index.
    bool remove(int index);  // Removes item at index.
    void clear();  // "clears" the list, but actually sets amount of items to zero.
    ItemType getIndex(int index);  // Returns the item at a given index.
    int find(const ItemType& findItem);  // Finds an item, then returns the index it was found.
    void printList() const;

private:
    struct Node // Struct so I can have my linked list.
    {
        ItemType item; // Data item.
        Node* next; // Pointer to the next node.
    };

    int itemCount; // Current amount of items in list.
    Node* headPtr; // Head/beginning of the list.

    Node* getNodeAt(int position) const; // Private method to get position.
};
#include "LinkedList.cpp"
#endif // LINKEDLIST_H

Then my cpp: 然后我的cpp:

#include "LinkedList.h"
#include <iostream>
using namespace std;

// All the other methods, and at the very end...

template<class ItemType>
Node* LinkedList<ItemType>::getNodeAt(int position) const //Error is here.
{
    Node* retPtr = headPtr;
    int index = 0;
    while(index != position)
    {
        retPtr = retPtr->next;
        index++;
    }
    return retPtr;
}

The error is at the method signature in the cpp file for getNodeAt. 错误发生在getNodeAt的cpp文件中的方法签名处。 From what I've read, it seems the errors come about when an object is referenced that isn't already defined, but I'm not really seeing how I've made that error. 从我阅读的内容来看,当引用一个尚未定义的对象时,似乎出现了错误,但是我并没有真正看到我是如何产生该错误的。

// All the other methods, and at the very end...

template<class ItemType>
Node* LinkedList<ItemType>::getNodeAt(int position) const //Error is here.
{
    Node* retPtr = headPtr;
    int index = 0;
    while(index != position)
    {
        retPtr = retPtr->next;
        index++;
    }
    return retPtr;
}

Node is a member struct of an encompassing struct, so do LinkedList<ItemType>::Node* in the return type as you've not yet entered the LinkedList<ItemType> scope. Node是包含结构的成员结构,返回类型中的LinkedList<ItemType>::Node*也是如此,因为您尚未进入LinkedList<ItemType>范围。

Also, if that template function is used by any other file directly (or through another template function), you'll probably have to move it to your header file at one point or you'll get another compiler error. 另外,如果该模板函数直接被任何其他文件使用(或通过另一个模板函数),则可能必须在某一时刻将其移动到头文件中,否则会出现另一个编译器错误。

The error is correct: there is no Node type anywhere in your program. 错误是正确的:程序中的任何地方都没有Node类型。 However there is a LinkedList<ItemType>::Node type. 但是,存在LinkedList<ItemType>::Node类型。 Use it instead. 改用它。

Another problem: you should not include LinkedList.cpp in LinkedList.h . 还有一个问题:你不应该包括LinkedList.cppLinkedList.h And certainly you should not include LinkedList.h in LinkedList.cpp if you do include .cpp file. 当然,如果您包含.cpp文件,则不应在LinkedList.cpp包括LinkedList.h General approach is to implement all template code in header. 通用方法是在标头中实现所有模板代码。 If you want to sepatate implementation and include it in header then do not include header in implementation and give it extention different from source code extention to not confuse build system. 如果要分离实现并将其包含在标头中,则不要在实现中包含标头,并使其扩展名不同于源代码扩展名,以免混淆构建系统。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM