0

I'm trying my hand at templates, and thought I'd try to make a linked list using them. I've got a header file and a cpp file.

Header file:

#ifndef LINKED_LIST_H
#define LINKED_LIST_H

template <class T>
class Node {
    public:
        // Constructor and desconstructor
        Node<T>(T value);
        ~Node();

        // ---- Methods ----
        void add(T value);            // Add the value at the end of the list
        void add(T value, int index); //

        void remove(int index);  //
        void remove(T value); //

        bool     containts(T value); // Recursively check if our value it the supplied value
        Node<T>* at(int index);      // Return the Node at the given index

        int  size();  // Recursively get the size

        void print(); // Print the value and move to the next one

        Node* next;  // Next Node in the list
        T     value; // Value of the Node
};

template <class T>
class LinkedList {
    public:
        // Constructor and deconstructor
        LinkedList<T>();
        ~LinkedList<T>();

        // ---- Methods ----
        void add(T value);            // Add a new Node at the end of the list with value
        void add(T value, int index); // Add a new Node at the given index of the list with value

        void remove(int index); // Remove the Node at the given index
        void remove(T value);   // Remove any Nodes with the given value

        bool     contains(T value);     // If the List contains the supplied value
        Node<T>* operator[](int index); // Node at the specified index

        int  size();  // Returns the number of Nodes in the list
        bool empty(); // What do you think this does?

        void print(); // Prints all the values in the List

    private:
        Node<T>* head;   // The top of the List
        Node<T>* latest; // Latest Node added
};

#endif

In my .cpp file, I try to define the Node's constructor using

#include "LinkedList.h"

template<class T> Node<T>::Node<T>(T value) {

}

However, on compilation I get this error:

./src/Util/LinkedList.cpp:3:19: error: 'Node::Node' names the constructor, not the type template Node::Node(T value) { ^ ./src/Util/LinkedList.cpp:3:19: error: and 'Node' has no template constructors

I shouldn't be defining the type should I? Since it's a template? Sorry if this is formatted badly, this is my first time using stack overflow.

Piotr Skotnicki
  • 46,953
  • 7
  • 118
  • 160
hdt80
  • 639
  • 1
  • 8
  • 14
  • 4
    1st of all: http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – πάντα ῥεῖ Jan 06 '15 at 20:56
  • 3
    `Node::Node` should just be `Node::Node`. In fact, inside the class body there is the *injected class name* the refers to the current instantiation of the class, so you can just use `Node` when inside the class body. And to answer your next question, [put the template definitions in the header files](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file). – David G Jan 06 '15 at 20:56
  • Your constructor *definition* must be in the header *except when you declare a template specialization which results in a complete type (eg, empty angle brackets)*. – inetknght Jan 06 '15 at 20:57

1 Answers1

5

The correct syntax you want is:

Node(T value); // not a template

And:

template<class T> Node<T>::Node(T value) {

The Node constructor itself is not a template, it takes a T - which is the class' template type. Now, if you wanted the constructor itself to be templated, that would look like:

template <typename U>
Node(U );

template <typename T>
template <typename U> // yep, twice
Node<T>::Node(U value)
{
    ..
}
Barry
  • 286,269
  • 29
  • 621
  • 977