Thursday 8 November 2018

c++ - Defining a constructor that uses templates

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 Node {

public:
// Constructor and desconstructor
Node(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* 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 LinkedList {
public:
// Constructor and deconstructor
LinkedList();
~LinkedList();


// ---- 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* 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* head; // The top of the List
Node* latest; // Latest Node added
};


#endif


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



#include "LinkedList.h"

template Node::Node(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 .

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...