For my homework assignment for my C++ class, I have to create a Linked List data structure. I have two classes at the moment. The List class (which is a template class) and the Link class. The Link class is nested within the List class, however, I am trying to define it in a separate header file. The issues I am having come from my lack of understanding in how the linking process works. Here is what I have.
Contents of List.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_
Contents of Link.h
#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_
Contents of 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’m sure this is a linking error that’s occurring. Similar to this error on Microsoft’s site that I looked up (http://msdn.microsoft.com/en-us/library/4ce3zbbc.aspx), however, I’m not sure how to resolve it.
With how the
#includes are done, the compiler isn’t looking atLink.h– so it can’t find and generate the class it needs.