简体   繁体   中英

Unknown type error in C++ when I return Node *

When I compile, I got " error: unknown type name 'Node'"

It comes from *Node * findKthElementToLast(int k)* function since it returns Node * type

I am not able to solve it. Any help would be nice!

LinkedList.h

#ifndef LINKEDLIST_H
#define LINKEDLIST_H

#include <cstdlib>
#include <iostream>

using namespace std;

template<class DataType>
class LinkedList{

struct Node {
    DataType dataIn;
    Node * next;
};

public:
    LinkedList();
   ~LinkedList();
    bool insertHead(DataType dataNew);
    bool insertTail(DataType dataNew);
    bool removeHead();
    bool removeTail();
    bool remove(DataType dataOut);
    bool removeDuplicates();
    bool removeDuplicatesUsingHash();
    bool search(DataType dataSearch);
    Node * findKthElementToLast(int k);
    void printLinkedList();

private:
   Node * head;

};
#endif

findKthElementToLast function from LinkedList.cpp

template<class DataType>
Node * LinkedList<DataType>::findKthElementToLast(int k){

    if(head == NULL){
     return head;
    } 
    Node * iter1 = head;
    Node * iter2 = head;

    int counter = 1;

    while(counter < k){
        counter++;
        iter2 = iter2->next;
    }
    while(iter2->next != NULL){
        iter1 = iter1->next;
        iter2 = iter2->next;
    }  
    return iter1;
}

Should be

template<class DataType>
LinkedList<DataType>::Node *LinkedList<DataType>::findKthElementToLast(int k) {

Node is declared inside the LinkedList class, so any references from outside that class must be qualified.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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