template using another template - c++

Here is a cut-down version of a template List code (adapted from http://www.daniweb.com/software-development/cpp/threads/237391/c-template-linked-list-help)
List complains (compile error) that "Node is not a type". Why is this, and what is the fix?
I tried replacing the "class Node" with a "struct Node" (and related changes), and the struct version worked fine. So the main question seems to be: how does a template-class access another template-class?
#include <iostream>
using namespace std;
template <typename T>
class Node
{
public:
Node(){}
Node(T theData, Node<T>* theLink) : data(theData), link(theLink){}
Node<T>* getLink( ) const { return link; }
const T getData( ) const { return data; }
void setData(const T& theData) { data = theData; }
void setLink(Node<T>* pointer) { link = pointer; }
private:
T data;
Node<T> *link;
};
template <typename T>
class List {
public:
List() {
first = NULL;
last = NULL;
count = 0;
}
void insertFirst(const T& newData) {
first = new Node(newData, first);
++count;
}
void printList() {
Node<T> *tempt;
tempt = first;
while(tempt != NULL){
cout << tempt->getData() << " ";
tempt = tempt->getLink();
}
}
~List() { }
private:
Node<T> *first;
Node<T> *last;
int count;
};
int main() {
List<int> myIntList;
cout << "Inserting 1 in the list...\n";
myIntList.insertFirst(1);
myIntList.printList();
cout << endl;
List<double> myDoubleList;
cout << "Inserting 1.5 in the list...\n";
myDoubleList.insertFirst(1.5);
myDoubleList.printList();
cout << endl;
}

You are using
new Node(newData, first);
within the List template. At that point, Node does not refer to a type, but to a template. But of course to create an instance of a type with new, you need a type there.
The most probable thing you want to do is to make it a type by instantiating the template, i.e.
new Node<T>(newData, first);

Related

Using Walter Savitch Linked List Interface

I am trying to learn data structures and I started with single linked list. I am first trying to just memorize and understand the code that Walter Savitch provided in Ch.13 of Absolute C++. I am having some trouble using the interface that the author designed for a single linked list. Specifically, I understand the code but I do not understand how to use the interface to create a single linked list in terms of the syntax needed.
Here is the interface:
#ifndef LinkedList_hpp
#define LinkedList_hpp
namespace LinkedList {
template<class T>
class Node {
private:
T data;
Node<T> *link;
public:
Node(const T& theData, Node<T>* theLink) : data(theData), link(theLink) {}
Node<T>* getLink() {return link;}
const T getData() const {return data;}
void setData(const T& theData) {data = theData;}
void setLink(Node<T>* pointer) {link = pointer;}
};
template<class T>
void headInsert(Node<T>*& head, const T& theData) {
head = new Node<T>(theData, head);
}
template<class T>
void insert(Node<T>* next, const T& theData) {
next->setLink(new Node<T>(theData, next->getLink( )));
}
template<class T>
void deleteNode(Node<T>* before) {
Node<T>* discard;
discard = before->getLink();
before->setLink(discard->getLink( ));
delete discard;
}
template<class T>
void deleteFirstNode(Node<T>*& head) {
Node<T>* discard;
discard = head;
head = head->getLink();
delete discard;
}
template<class T>
Node<T>* search(Node<T>* head, const T& target) {
Node<T>* here = head;
if(here == nullptr) {
return nullptr;
}
else {
while(here->getData() != target && here->getLink() != nullptr)
here = here->getLink();
if(here->getData() == target)
return here;
else
return nullptr;
}
}
}
#endif /* LinkedList_hpp */
Now I am trying to just use the interface to create a single linked list. But I am having some trouble. Here is how far I have gone.
#include <iostream>
#include "LinkedList.hpp"
using namespace LinkedList;
int main(int argc, const char * argv[]) {
Node<int> test(2,nullptr);
return 0;
}
Can anyone help me just show me what I need to write into the int main() body to create a single linked list and perhaps print its contents? Just for ease of simplicity I want to try to make a single linked list look like this:
Here's how that interface could be accessed:
Node<int> node(20,nullptr);
Node<int> *head = &node;
insert(head, 30);
prnt(head);
std::cout << "Searching for 10..." << search(head, 10) << std::endl;
headInsert(head, 10);
std::cout << "Searching for 10..." << search(head, 10) << std::endl;
prnt(head);
deleteFirstNode(head);
prnt(head);
deleteNode(head);
prnt(head);
Included is prnt method, which traverses the current list from the head until it finds a nullptr, printing the data at each node and helps demonstrate what each method does. The method is as follows:
template<class T>
void prnt(Node<T>* head) {
for (; head->getLink(); head=head->getLink()){
std::cout << head->getData() << " -> ";
}
std::cout << head->getData() << std::endl;
}

Reversing Linked List with Recursion, using STL

Code for Reversing Linked List with Recursion, using STL
#include<iostream>
#include<conio.h>
#include<list>
using namespace std;
template<typename T>
class node
{
public:
T data;
node<T> *next;
node(){ next = NULL; }
node(const T& item, node<T> *nextnode = NULL)
{
data = item;
next = nextnode;
}
};
template<typename T>
class Reverse_list
{
private:
node<T> *head;
void reverse(node<T> *front);
public:
Reverse_list(){ head = NULL; }
//template<typename T>
void Reverse();
template<typename T>
void Display( list<T>& alist );
};
int main()
{
Reverse_list <int> rl;
list<int> intlist;
int size, no;
cout << "Size of List ?? ";
cin >> size;
for (int i = 1; i <= size; i++)
{
cout << "Enter the " << i <<" "<< "element";
cin >> no;
intlist.push_front(no);
}
rl.Display(intlist);
rl.Reverse();
rl.Display(intlist);
_getch();
return 0;
}
template<typename T>
void Reverse_list<T>::Display(list<T>& alist)
{
list<int>::iterator iter = alist.begin();
while (iter != alist.end())
{
cout << *iter << " ";
iter++;
}
}
template<typename T>
void Reverse_list<T>::reverse(node<T> *front)
{
if (front->next == NULL)
{
head = front;
return;
}
reverse(front->next);
node<int> *back = front->next;
back->next = front;
front->next = NULL;
}
template<typename T>
void Reverse_list<T>::Reverse()
{
reverse(head);
}
The above code generates 2 errors.
Error 1) No instance of function template matches the argument list. ( No error number.)
If I remove line 1 ( mentioned in a code ) then above error is no more. ( Why? )
Error 2) C2783: 'void Reverse_list::Reverse1(void)' : could not deduce template argument for 'T'
How to solve above errors.
In above program , I wanted to pass " head" node ( which is private ) as
argument to Reverse function. But we can not access private member outside of the class. So I passed indirectly. Is this a correct way of passing ?? Or there is some other way of accessing private data ??
I'm not sure to understand your intentions but...
You're trying to declare a method (reverse()) inside another method (Reverse()) ? Uhmmm....
We return to this later.
Imagine that the following instruction is correct instruction of Reverse_list<T>::Reverse()
node<T> *back = front->next;
Why you declare back as a pointer to a generic Node<T> when you assign front->next (so a specific Node<int>) to it?
If you define back as a node<int> pointer, the method Reverse() has no longer reason to be a template (dependant from T) method. And you can avoid both errors.
With your actual code, when you call
rl.Reverse();
you call a template method but the compiler doesn't know how to determine the type T. You could explicit it in this way
rl.Reverse<int>();
but, as written before, I thik it's better if you remove the whole template part.
Or, alternatively, you can transform the whole class in a template class; where head is a pointer to a generic Node<T>, not a specifica Node<int>.
Something like (if I understand correctly your intentions)
template <typename T>
class Reverse_list
{
private:
node<T> *head;
void reverse (node<T> * front);
public:
Reverse_list() : head(NULL)
{ }
void Reverse();
void Display(list<T>& alist);
};
template<typename T>
void Reverse_list<T>::reverse (node<T> * front)
{
if (front->next == NULL)
{
head = front;
return;
}
reverse(front->next);
node<T> *back = front->next;
back->next = front;
front->next = NULL;
}
template<typename T>
void Reverse_list<T>::Reverse()
{ reverse(head); }
In this case, in main(), rl should be declared as
Reverse_list<int> rl;
fixing T as int, and the call to Reverse() should be
rl.Reverse();
--- EDIT 2016.05.10 ---
With the "template Reverse_list" solution, you should correct three points (at last).
1) in Reverse_list class declaration, you have commented the template<typename T> row before void Reverse(); good; you should delete (comment) the same line (for the same reason) before void Display( list<T>& alist );; so the class become
template<typename T>
class Reverse_list
{
private:
node<T> *head;
void reverse(node<T> *front);
public:
Reverse_list(){ head = NULL; }
//template<typename T>
void Reverse();
//template<typename T>
void Display( list<T>& alist );
};
2) Display() now is a method of a templated class; so the line
list<int>::iterator iter = alist.begin();
become
list<T>::iterator iter = alist.begin();
3) reverse() now is a method of a templated class; so the line
node<int> *back = front->next;
become
node<T> *back = front->next;

C++ Segmentation Fault: Passing a string to a node in a linked list

I'm new to working with class templates and am simply trying to define a temporary node 'temp' in a class associated with the Linked List, which sets the string that the node stores to some temporary string that is created in the function TowerHanoi::set_Discs(size_t disc) via user input. When I call the function temp->set_data(tmp_str) i get a segmentation fault. I tried calling temp->set_data("hello"); on its own and i still get the error.
I'm not sure what's going on here and i've tried researching into it but to no avail. I'm probably missing something obvious, but i'm just quite lost now. Let me know if you need more code. Thanks.
TowerHanoi.cpp:
#include "TowerHanoi.h"
#include <iostream>
#include <cstdlib>
using namespace std;
using oreilly_A2::node;
namespace oreilly_A2 {
TowerHanoi::TowerHanoi() {
for (int i=0;i<2;i++) {
rod[i] = LStack<node<std::string> >();
}
}
TowerHanoi::TowerHanoi(size_t numDiscs) {
for (int i=0; i < 2; i++) {
rod[i] = LStack<node<string> >();
}
discs = numDiscs;
}
void TowerHanoi::set_Discs(size_t disc) {
node<string>* temp=NULL;
while (disc != 0) {
string tmp_str;
for (size_t i=0; i<disc; i++) {
tmp_str.append("x");
}
disc--;
temp->set_data(tmp_str);
rod[0].push(temp);
}
void TowerHanoi::print_Game() {
for (size_t s=1; s<discs; s++) {
cout << " ";
for (size_t o=1; o<discs-s;o++) {
cout << " ";
}
//cout << tmp_node->data() << endl;
cout << "x" << endl;
}
}
}
node.h file:
#ifndef NODE_CAMERON_H
#define NODE_CAMERON_H
#include <string>
namespace oreilly_A2 {
template <typename Item>
class node {
public:
node(); //constructor for node
node(const Item val, node* newNext); //constructor with parameters
~node(); //destructor
void set_data(Item new_data); //set the word that this node contains
void set_link(node* new_link); //set the 'next' node
void set_previous(node* new_prev);
Item data() const; //return this node's word
const node* link() const; //return next
const node* back() const;
node* link(); //return next
node* back();
private:
node* next; //the next node
node* previous;
Item word; //the word this node contains
};
}
#include "Node.template"
#endif
node.template file:
namespace oreilly_A2 {
template <typename Item>
node<Item>::node() {
next=NULL;
previous=NULL;
}
//Node.template
template <typename Item>
node<Item>::node(const Item val, node* newNext=NULL) {
word = val;
next = newNext;
}
template <typename Item>
node<Item>::~node() {
delete next;
delete previous;
delete word;
}
template <typename Item>
void node<Item>::set_data(Item new_data){
word = new_data;
}
template <typename Item>
void node<Item>::set_link(node* new_link){
next = new_link;
}
template <typename Item>
void node<Item>::set_previous(node* new_back) {
previous = new_back;
}
template <typename Item>
Item node<Item>::data() const { //return the word
return word;
}
template <typename Item>
const node<Item>* node<Item>::link() const { //return next node (const function)
return next;
}
template <typename Item>
const node<Item>* node<Item>::back() const { //return previous node (const)
return previous;
}
template <typename Item>
node<Item>* node<Item>::link() {
return next; //return next node (non-const)
}
template <typename Item>
node<Item>* node<Item>::back() { //return previous node (const)
return previous;
}
}
Unless I have missed something the temp variable is NULL at the time of calling set_data. As any regular object you need to first initialized it.
node<string>* temp=new node<string>();
And then freeing it when appropriate to avoid memory leaks.
This is not the case with temp_str because the later is not a pointer, it's a value so it gets initialized automatically (and also freed automatically when it gets out of scope).
You have initialized temp as NULL. So when you are trying to do temp->set_data(tmp_str); you are actually trying to access NULL pointers.
All you need to do is initialize temp. I have correct the code below
void TowerHanoi::set_Discs(size_t disc) {
node<string>* temp=new node<string>();
while (disc != 0) {
string tmp_str;
for (size_t i=0; i<disc; i++) {
tmp_str.append("x");
}
disc--;
temp->set_data(tmp_str);
rod[0].push(temp);
}
To avoid memory leak you need to delete all the memory allocated after you are done.

Pointer to template class object

I'm trying to create a individual List with a templated value, but unfortunately I can not link from my List to the ListElements with templates.
In my main I call List<int> list1; to create a instance of the class List.
A List contains multiple ListElements that contain the value, that should be templated.
Compiler throws an error at
ListElement* first;
ListElement* last;
in List.h.
It says C2955 - 'ListElement' : use of class type requires type argument list
List.h
#pragma once
#include <string>
#include "ListElement.h"
template<class T>
class List
{
private:
ListElement* first;
ListElement* last;
public:
List();
~List();
void printList();
void pushBack(T value);
void pushFront(T value);
};
List.cpp
#include <iostream>
#include "List.h"
template<class T>
List<T>::List()
{
first = NULL;
last = NULL;
}
template<class T>
List<T>::~List()
{
}
template<class T>
void List<T>::pushBack(T value)
{
if (last)
{
ListElement* tmp = last;
last = new ListElement(value);
last->setPrev(tmp);
tmp->setNext(last);
}
else
{
first = new ListElement(value);
last = first;
}
}
template<class T>
void List<T>::pushFront(T value)
{
if (first)
{
ListElement* tmp = first;
first = new ListElement(value);
first->setNext(tmp);
tmp->setPrev(first);
}
else
{
last = new ListElement(value);
first = last;
}
}
template<class T>
void List<T>::printList()
{
if (first)
{
ListElement* tmp = first;
while (tmp)
{
std::cout << tmp->getValue() << std::endl;
if (tmp != last)
tmp = tmp->getNext();
else
break;
}
}
else
{
std::cout << "List is empty!" << std::endl;
}
}
template class List<int>;
template class List<std::string>;
ListElement.h
#pragma once
#include <string>
template<class T>
class ListElement
{
private:
ListElement* next;
ListElement* prev;
T value;
public:
ListElement(T val);
~ListElement();
ListElement* getNext() { return next; }
ListElement* getPrev() { return prev; }
void setNext(ListElement* elem) { next = elem; }
void setPrev(ListElement* elem) { prev = elem; }
T getValue() { return value; }
};
ListElement.cpp
#include "ListElement.h"
template<class T>
ListElement<T>::ListElement(T val)
{
value = val;
}
template<class T>
ListElement<T>::~ListElement()
{
}
template class ListElement<int>;
template class ListElement<std::string>;
ListElement is a template, so you want to use a specific instantiation for your pointers:
template<class T>
class List
{
private:
ListElement<T>* first;
ListElement<T>* last;
// note: ^^^
likewise for other occurrences. Only within the template, the template name is usable for the current instantiation, i.e., within List, you can use List as a shortcut for List<T>.

Implicit instantiation of undefined template

Here's a template (queue) I'm trying to write:
#include <iostream>
using namespace std;
template <typename T>
class Queue
{
friend ostream& operator<< (ostream &, const Queue<T> & );
private:
template<class> class Node;
Node<T> *front;
Node<T> *back;
public:
Queue() : front(0), back(0) {}
~Queue();
bool Empty()
{
return front == 0;
}
void Push(const T& NewEl)
{
Node<T&> *El = new Node<T> (NewEl);
if (Empty())
front=back=El;
else
{
back-> next = El;
back = El;
}
}
void Pop()
{
if (Empty())
cout << "Очередь пуста." << endl;
else
{
Node<T> *El = front;
front = front -> next;
delete El;
}
}
void Clear()
{
while (! Empty())
Pop();
}
};
template <typename T>
class Node
{
friend class Queue<T>;
public:
Node() {next = 0;}
Node(T nd) {nd=node; next=0;}
T& getsetnode(){return node;}
Node<T>*& getsetnext(){return next;}
private:
T front;
T back;
T node;
Node<T> *next;
};
template <class T> ostream& operator<< (ostream &, const Queue<T> & );
int main()
{
Queue<int> *queueInt = new Queue<int>;
for (int i = 0; i<10; i++)
{
queueInt->Push(i);
cout << "Pushed " << i << endl;
}
if (!queueInt->Empty())
{
queueInt->Pop();
cout << "Pop" << endl;
}
queueInt->Front();
queueInt->Back();
queueInt->Clear();
cout << "Clear" << endl;
return 0;
}
At these lines:
Node<T&> *El = new Node<T> (NewEl);
front = front -> next;
delete El;
I get Implicit instantiation of undefined template 'Queue<int>::Node<int>'. What am I doing wrong? After reading this post I tried changing int to const int to see if that was the problem, but apparently it isn't, because I get the same error.
I'm using XCode with LLVM compiler 4.2. When I switch to GCC I get more errors:
template<class> class Node; gets Declaration of 'struct Queue<int>::Node<int>',
Node<T&> *El = new Node<T> (NewEl); gets Invalid use of incomplete type,
and anything dealing with assignment of anything to El can't convert <int&>* to <int>* (but deleting reference doesn't change anything for LLVM).
template <typename T>
class Queue
{
private:
template<class> class Node;
/* ... */
This is a forward declaration of Queue::Node. The latter defined class Node is in the global namespace, so they aren't the same and any usage of Queue::Node will result in an incomplete-type error. Since you don't provide an interface to the interior nodes anyway just scrap the global definition of Node and stick it into Queue:
template <typename T>
class Queue
{
private:
class Node
{
public:
Node() {next = 0;}
/* ... */
};
/* ... */
};