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.
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)
{
..
}
Related
is there a way to pass in an extra argument to my function pointer in BST? I am trying to use BST inOrder to get value from a map<string, int>. this BST will be storing the key of the map.
The map will act as a database that uses date + time as the key. each BST will be created to store the date+time of each year and saved into another map (bstMap) which holds all bst. bstMap will use the year as key.
BST inOrder with function ptr.
#ifndef BST_H
#define BST_H
#include<iostream>
using namespace std;
template <class T>
class Node.
{
public:
T m_key;
Node<T> *m_left;
Node<T> *m_right;
};
template <class T>
class BST
{
typedef void(*funcPtr)(T &);
public:
BST();
void Insert(T key);
void Delete();
void InOrder(void(*funcPtr)(T &)) const;
void PreOrder(void(*funcPtr)(T &)) const;
bool Search(T key);
T MaxValue();
bool IsEmpty() const {return m_root == nullptr;}
void DeleteTree();
private:
Node<T> *m_root;
protected:
Node<T> *Insert(Node<T>* node, T key);
Node<T> *Search(Node<T>* node, T key);
void InOrder(Node<T>* node, void (*funcPtr)(T &)) const;
void PreOrder(Node<T>* node, void (*funcPtr)(T &)) const;
void DeleteTree(Node<T>* node);
Node<T>* MaxValue(Node<T>* node);
};
template<class T>
BST<T>::BST(){
m_root = nullptr;
}
template<class T>
void BST<T>::InOrder(Node<T>* node, void(*funcPtr)(T &)) const
{
if (node != nullptr)
{
InOrder(node-> m_left, funcPtr); //recursive call for node left
funcPtr(node-> m_key);
InOrder(node->m_right, funcPtr);
}
}
template<class T>
void BST<T>::InOrder(void(*funcPtr)(T &)){
InOrder(m_root, funcPtr);
}
This line of code is called from main.cpp which pass the user input year into the map to return the bst which stores all relevant keys.
void GetData(string& year, map<string, BST<string>>& bstMap)
{
BST<string> bstKey = bstMap[year];
bstKey.InOrder(&GetTotal);
}
So here is where i am stuck..
void GetTotal(string& key) <- how do i reference my database map here?
{
cout<< key <<endl;
}
If you want to access variables outside of the BST template class (such as the map), then I advise changing your template to the following (assuming that m_root is a member variable of BST<T>, and that it is the root of tree):
template<class T, class Fn>
void BST<T>::InOrder(Fn funcPtr) const
{
InOrder(m_root, fn);
}
template<class T, class Fn>
void BST<T>::InOrder(Node<T>* node, Fn funcPtr) const
{
if (node)
{
InOrder(node-> m_left, funcPtr); //recursive call for node left
funcPtr(node-> m_key);
InOrder(node->m_right, funcPtr);
}
}
Then this way, you can pass a function object or lambda that knows about the map. In the case below, a lambda function is used:
void GetData(string& year, map<string, BST<string>>& bstMap)
{
BST<string> bstKey = bstMap[year];
bstKey.InOrder([&](std::string& key) { std::cout << bstMap[key] << "\n"; });
}
The above provides a lambda that captures the passed-in map parameter.
So i am coding a browser tab structure, and i have 5 custom class, Stack, LinkedList, Node(for linked list), Tab and Browser.
LinkedList and Stack works fine on their own, but when i construct a LinkedList in Browser's constructer i get error. So in my main i only call Browser().Here are the codes:
LinkedList<T>::LinkedList(){
front=NULL;
back=NULL;
}`
Node<T>::Node(){
prev=NULL;
next=NULL;
data=T();
}
Stack<T>::Stack(int capacity){ //capacity is optional, this is the default constructor.
this->capacity=capacity;
this->size=0;
this->items=new T[capacity];
}
`
Browser(){
selected = NULL;
//print browser link construction starts
pages= LinkedList<Tab>(); //This line gives the error.
closedPages= Stack<Tab>();
curr_index=-1;
tab_count=0;
}
Tab(){
current_page="";
prevPages=Stack<string>();
nextPages=Stack<string>();
closed_index=-1;
}
What is even funnier is that when i do print insertion, what i see is it first starts and finishes a link construction(without doing any tab construction in between), then does bunch of stack and tab construction, THEN prints the "browser link construction starts" and then goes in to and finishes another link construction and then gives seg fault. So it goes in to link construction twice even tough i hoped it would do only once?
Thanks, and sorry in advance if it is something extremly easy/stupid.
EDIT1: Full LinkedList Code
#ifndef _LINKEDLIST_H_
#define _LINKEDLIST_H_
#include <iostream>
#include <cstddef>
#include <stdexcept>
#include "Node.hpp"
using namespace std;
template <class T>
class LinkedList {
private:
/* pointer to the first node */
Node<T>* front;
/* pointer to the last node */
Node<T>* back;
public:
LinkedList();
LinkedList(const LinkedList<T>& ll);
LinkedList<T>& operator=(const LinkedList<T>& ll);
~LinkedList();
/* returns the first node of the linked list */
Node<T>& getFront() const;
/* returns the last node of the linked list */
Node<T>& getBack() const;
/* returns the node in given "pos"ition of the linked list */
Node<T>& getNodeAt(int pos) const;
/* returns the pointer of the node in given
"pos"ition of the linked list */
Node<T>* getNodePtrAt(int pos) const;
/* inserts a new node containing "data"
after the node "prev"
*/
void insert(Node<T>* prev, const T& data);
/* inserts a new node containing "data"
at "pos"ition in the linked list
*/
void insertAt(int pos, const T& data);
/* erases the given "node" from the linked list */
void erase(Node<T>* node);
/* erases the node in given "pos"ition from the linked list */
void eraseFrom(int pos);
/* clears the contents of the linked list */
void clear();
/* inserts a new node containing "data"
to the front of the linked list
*/
void pushFront(const T& data);
/* inserts a new node containing "data"
to the back of the linked list
*/
void pushBack(const T& data);
/* removes the first node */
void popFront();
/* removes the last node */
void popBack();
/* returns true if the list is empty, false otherwise */
bool isEmpty() const;
/* returns the number of items in the list */
size_t getSize() const;
/* prints the contents of the linked list
one node data per line
assumes the objects in the node have operator<< overloaded
*/
void print() const;
};
template <class T>
LinkedList<T>::LinkedList(){
cout << "list construction starts" << endl;
front=NULL;
back=NULL;
cout << "list construction ends" << endl;
}
//COPY AND ASSIGMENT
template<class T>
LinkedList<T>::LinkedList(const LinkedList<T>& ll){
*this=ll;
}
template<class T>
LinkedList<T>& LinkedList<T>::operator=(const LinkedList<T>& ll){
clear();
Node<T>* temp=&(ll.getFront());
while(temp->getNext()!=NULL){
pushBack(temp->getData());
temp->setNext(temp->getNext());
}
pushBack(temp->getData());
return *this;
}
template<class T>
LinkedList<T>::~LinkedList(){
clear();
}
template <class T>
Node<T>& LinkedList<T>::getFront() const{
return *front;
}
template <class T>
Node<T>& LinkedList<T>::getBack() const{
return *back;
}
template <class T>
Node<T>& LinkedList<T>::getNodeAt(int pos) const{
if(pos<0 or pos>=getSize()){
throw out_of_range("Bad Input");
}
Node<T>* retval=front;
for(int i=0;i<pos;i++){
retval=retval->getNext();
}
return *retval;
}
template <class T>
Node<T>* LinkedList<T>::getNodePtrAt(int pos) const{
if(pos<0 or pos>getSize()){
throw out_of_range("Bad Input");
}
Node<T>* retval=front;
for(int i=0;i<pos;i++){
retval=retval->getNext();
}
return retval;
}
template <class T>
void LinkedList<T>::insert(Node<T>* prev,const T& data){
if(prev==NULL){
pushFront(data);
}
else if(prev==back){
pushBack(data);
}
else{
Node<T>* newNode=new Node<T>();
newNode->setData(data);
prev->getNext()->setPrev(newNode);
newNode->setNext(prev->getNext());
newNode->setPrev(prev);
prev->setNext(newNode);
}
}
template <class T>
void LinkedList<T>::insertAt(int pos,const T& data){
if(pos==0){
pushFront(data);
}
else if(pos==getSize()){
pushBack(data);
}
else{
Node<T>* tmp=getNodePtrAt(pos);
Node<T> newNode;
newNode.setData(data);
tmp->getPrev()->setNext(&newNode);
newNode.setNext(tmp);
newNode.setPrev(tmp->getPrev());
tmp->setPrev(&newNode);
}
}
template <class T>
void LinkedList<T>::pushFront(const T& data){
Node<T>* newNode=new Node<T>();
newNode->setData(data);
if(front==NULL){
front=newNode;
back=newNode;
}
else {
newNode->setNext(front);
front->setPrev(newNode);
front=newNode;
newNode->setPrev(NULL);
}
}
template <class T>
void LinkedList<T>::pushBack(const T& data){
Node<T>* newNode=new Node<T>();
newNode->setData(data);
if(front==NULL){
front=newNode;
back=newNode;
}
else {
newNode->setPrev(back);
back->setNext(newNode);
back=newNode;
newNode->setNext(NULL);
}
}
template <class T>
void LinkedList<T>::erase(Node<T>* node){
if(node==front){
popFront();
}
if(node==back){
popBack();
}
else {
node->getNext()->setPrev(node->getPrev());
node->getPrev()->setNext(node->getNext());
node->setNext(NULL); node->setPrev(NULL);
}
delete node;
}
template <class T>
void LinkedList<T>::eraseFrom(int pos){
Node<T>* tmp=getNodePtrAt(pos);
erase(tmp);
}
template <class T>
void LinkedList<T>::clear(){
while(!isEmpty()){
popFront();
}
}
template <class T>
void LinkedList<T>::popFront(){
Node<T>* tmp;
tmp=front;
if(front==back){
front=NULL;
back=NULL;
}
else{
front=front->getNext();
front->setPrev(NULL);
}
delete tmp;
}
template <class T>
void LinkedList<T>::popBack(){
Node<T>* tmp;
tmp=back;
if(front==back){
front=NULL;
back=NULL;
}
else {
back=back->getPrev();
back->setNext(NULL);
}
delete tmp;
}
template <class T>
bool LinkedList<T>::isEmpty() const{
return front==NULL;
}
template <class T>
size_t LinkedList<T>::getSize() const{
if(front==NULL){
return 0;
}
size_t size=1;
Node<T>* current=front;
while(current->getNext()!=NULL){
size++;
current=current->getNext();
}
return size;
}
template <class T>
void LinkedList<T>::print() const{
Node<T>* current=front;
if(front!=NULL){
while(current->getNext()!=NULL){
cout << current->getData() << endl;
current=current->getNext();
}
cout << current->getData() << endl;
}
}
#endif
Your assignment operator appears to be problematic. In your Browser constructor I see this:
pages = LinkedList<Tab>();
This is actually unnecessary but is triggering your problem. Your 'pages' variable is already default constructed (which is why you see all that activity before the print statement) by the time your Browser constructor code begins. Then you perform the assignment which utilizes your assignment operator.
Your assignment operator is assuming that the source LinkedList object has data, but it does not. It is calling getFront() which dereferences 'front', which is NULL. You should check if it is empty first, which I see you have a method for already (isEmpty()).
I have a header file for a program that utilizes singly linked list. The data that is originally stored in the nodes were integers, however, in an attempt to use the template class, I tried to convert the Node class and AnyList class to template classes; however, when I compile, an error message that says "'Node": use of class template requires template argument list" appears. I've seen examples of template classes, but there's a bit of confusion since I'm trying to make two template classes in one header file.
#ifndef ANYLIST_H
#define ANYLIST_H
#include<iostream>
#include <string>
using namespace std;
template <typename T>
class Node
{
public:
Node() : data(0), next(NULL) {}
Node(T& theData, Node *newNext) : data(theData), next(newNext){}
Node* getNext() const { return next; }
T getData( ) const { return data; }
void setData(T& theData) { data = theData; }
void setNext(Node *newNext) { next = newNext; }
~Node(){}
private:
T data;
Node *next; //pointer that points to next node
};
template <typename T>
class AnyList
{
friend ostream& operator<<(ostream& out, const AnyList<T>& theList);
public:
AnyList();
void insert(const T& elem);
int getNumOfElem() const;
void destroyList();
~AnyList();
private:
Node *first;
int count;
};
#endif
Simply put, Node<> and List<> are different templates. So you need to forward the template parameter from List to Node.
Replace
Node *first;
By
Node<T> *first;
I'm getting the compiler error: error "Node" does not name a type.
This is my header:
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
template <class ItemType>
class LinkedList
{
public:
LinkedList(); // Constructor
bool isEmpty() const; // Checks if list is empty.
int getLength() const; // Returns the amount of times in the list.
bool insert(int index, const ItemType& insertItem); // Inserts an item at index.
bool remove(int index); // Removes item at index.
void clear(); // "clears" the list, but actually sets amount of items to zero.
ItemType getIndex(int index); // Returns the item at a given index.
int find(const ItemType& findItem); // Finds an item, then returns the index it was found.
void printList() const;
private:
struct Node // Struct so I can have my linked list.
{
ItemType item; // Data item.
Node* next; // Pointer to the next node.
};
int itemCount; // Current amount of items in list.
Node* headPtr; // Head/beginning of the list.
Node* getNodeAt(int position) const; // Private method to get position.
};
#include "LinkedList.cpp"
#endif // LINKEDLIST_H
Then my cpp:
#include "LinkedList.h"
#include <iostream>
using namespace std;
// All the other methods, and at the very end...
template<class ItemType>
Node* LinkedList<ItemType>::getNodeAt(int position) const //Error is here.
{
Node* retPtr = headPtr;
int index = 0;
while(index != position)
{
retPtr = retPtr->next;
index++;
}
return retPtr;
}
The error is at the method signature in the cpp file for getNodeAt. From what I've read, it seems the errors come about when an object is referenced that isn't already defined, but I'm not really seeing how I've made that error.
The error is correct: there is no Node type anywhere in your program. However there is a LinkedList<ItemType>::Node type. Use it instead.
Another problem: you should not include LinkedList.cpp in LinkedList.h. And certainly you should not include LinkedList.h in LinkedList.cpp if you do include .cpp file. General approach is to implement all template code in header. If you want to sepatate implementation and include it in header then do not include header in implementation and give it extention different from source code extention to not confuse build system.
// All the other methods, and at the very end...
template<class ItemType>
Node* LinkedList<ItemType>::getNodeAt(int position) const //Error is here.
{
Node* retPtr = headPtr;
int index = 0;
while(index != position)
{
retPtr = retPtr->next;
index++;
}
return retPtr;
}
Node is a member struct of an encompassing struct, so do LinkedList<ItemType>::Node* in the return type as you've not yet entered the LinkedList<ItemType> scope.
Also, if that template function is used by any other file directly (or through another template function), you'll probably have to move it to your header file at one point or you'll get another compiler error.
I am trying to implement a red black tree with the use of templates. For example, when inserting an item to the tree, the key and the item should both be generic types. Till now, I implemented a header file which consists of a struct and functions to be implemented. However, I don't know if I'm using templates the right way. Also, when I tried to implement the 'Insert' function, the IDE gives the error:
prototype for ‘void RedBlackTree::InsertKey(Item*&, Key*&)’ does not match any in class ‘RedBlackTree’ RedBlackTree.h
This is my header file:
#ifndef REDBLACKTREE_H_
#define REDBLACKTREE_H_
template <class Item, class Key>
class RedBlackTree
{
typedef enum
{
BLACK,
RED
}ColourNode;
typedef struct RBT
{
struct RBT *left;
struct RBT *right;
struct RBT *parent;
struct RBT *root;
ColourNode colour;
Item item;
Key key;
}RBTNode;
public:
~RedBlackTree(); // destructor
RedBlackTree(Item, Key); // default constructor
void InsertKey(Item, Key);
int InsertFixUp(Item, Key);
int RemoveKey(Item, Key);
int FindKey(Item, Key);
private:
RedBlackTree<Item, Key> *rootPointer;
RedBlackTree<Item, Key> *NILL_LEAF;
};
template <class Item, class Key>
void RedBlackTree<Item, Key>::InsertKey(Item *&T, Key *&z)
{
//node* nil=tree->nil;
//node* root=tree->root;
RBTNode *y;
RBTNode *x;
y=T->nil;
x=T->root;
while(x != T->nil)
{
y=x;
if((z->key)<(x->key))
x=x->left;
else
x=x->right;
}
y=z->parent;
if(y == T->nil)
z=T->root;
else
if((z->key)<(y->key))
z=y->left;
else
z=y->right;
z->left=T->nil;
z->right=T->nil;
z->colour=RED;
InsertFixUp(T,z);
}
#endif /* REDBLACKTREE_H_ */
Thanks in advance.
The problem is that the types of the arguments to InsertKey don't match the declaration. In the declaration the arguments are Item and Key, and in the implementation they are Item*& and Key*& (references to pointers). These need to match.
void InsertKey(Item, Key);
^^^^ ^^^
void RedBlackTree<Item, Key>::InsertKey(Item *&T, Key *&z)
^^^^^^^ ^^^^^^
You have to move the implementation of the function (the template) to the class definition.
template <class Item, class Key>
class RedBlackTree
{
//...
public:
~RedBlackTree(); // destructor
RedBlackTree(Item, Key); // default constructor
void InsertKey(Item *&T, Key *&z)
{
//...
}
//...
};