Associate errors with the C ++ template class and nested classes defined in separate header files

For my homework for my C ++ class, I need to create a Linked List data structure. At the moment I have two classes. List class (template class) and Link class. The Link class is nested in the List class, however I am trying to define it in a separate header file. The problems I'm experiencing are related to my lack of understanding of how the binding process works. Here is what I have.

List Content .h

#ifndef _LIST_H_
#define _LIST_H_

template <class T>
class List
{
protected:
  class Link;

public:
  List() : _head(nullptr) { }
  ~List() { }

  void PushFront(T object)
  {
    // !! Attention !!
    // When I uncomment this line I get the error:
    // error C2514: 'List<T>::Link' : class has no constructors...
    // My problem is the compiler doesn't know what Link is yet (I'm assuming).

    //_head = new Link(object, _head);
  }

protected:
  Link* _head;
};

#endif // _LIST_H_

Link.h Content

#ifndef _LINK_H_
#define _LINK_H_

#include "List.h"

template <class T>
class List<T>::Link
{
public:
  Link(T object, Link* next = nullptr)
    : _object(object), _next(next) { }
  ~Link() { }


private:
  T     _object;
  Link* _next;
};

#endif // _LINK_H_

Content main.cpp (entry point)

#include "List.h"

int main()
{
  int b = 5;
  List<int> a;
  a.PushFront(b); // If I comment this line, then the code compiles fine.
}

I am sure this is a communication error that is occurring. Similar to this error on the Microsoft website that I looked at ( http://msdn.microsoft.com/en-us/library/4ce3zbbc.aspx ), I'm not sure how to resolve it.

+3
1

#include Link.h - , .

+1

All Articles