1

i am trying to implement a LinkList class with a struct as my private member, however, when i try to define the struct ListNode under the LinkList.ccp, the compiler says that the pointer is not defined. The error is on the high lighted line

#ifndef LINKLIST_H
#define LINKLIST_H

#include <iostream>
using namespace std;

template <typename T>
class LinkList{
private:
    struct ListNode{
        T item;
        ListNode * next;
    };
    int size; // the size of the listNodes.
    ListNode* _head;

public:
    LinkList(); // constructor
    void giveup(int n);
    bool isEmpty();
    int getLength();

    ListNode * find(int index);
};

#endif

#include "LinkList.h"

template <typename T>
LinkList<T>::LinkList(){
    _head = NULL;
    size = 0;
}

template <typename T>
void LinkList<T>::giveup(int n){
    cout << "The error " << n << " has occured" << endl;
}

template <typename T>
bool LinkList<T>::isEmpty(){
    return (size == 0);
}

template <typename T>
int LinkList<T>::getLength(){
    return size;
}

template <typename T>
***ListNode * LinkList<T>::find(int index){ // pointer not defined***
    if(index < 1 || index > size){
        return NULL;
    }
    ListNode * currentNode = _head;
    for(int pos =2; pos <=index; pos++){
        currentNode = currentNode->next;
    }
    return currentNode;
}
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
progammingBeignner
  • 936
  • 1
  • 8
  • 19

1 Answers1

0

In a member function definition outside the class, a leading return type isn't scoped within the class, so you have to qualify it if it's a nested type:

template <typename T>
LinkList<T>::ListNode * LinkList<T>::find(int index)
^^^^^^^^^^^^^

The parameter list and any trailing return type is scoped within the class, so (in C++11 or later) you could write this as

template <typename T>
auto LinkList<T>::find(int index) -> ListNode *

Once you've fixed that then, in order to use the template, you'll need to move the function definitions into the header file, as described here.

Community
  • 1
  • 1
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • Thank you for the prompt reply. i input LinkList::ListNode * LinkList::find(int index) under the ccp file, however, it returns an error stating that missing 'typename' prior to dependent type name 'LinkList::ListNode'.. (and, any way i can correct this without using auto? ) Thanks – progammingBeignner Sep 16 '14 at 12:46