I am having a problem with my first attempt at a proper go at modular programming....
I was wondering if anyone could suggest a possible direction as to how to approach the solution to the problem
here is my code and once again it is from Laszlo's Book on computational geometry ..the only thing I did different was break it into smaller pieces and a unit header file
The Code
header.h
#ifndef NULL
#define NULL 0
#endif
#ifndef A_H
#define A_H
// -----Definition of Node Class ----------------------------------
class Node{
protected:
Node *_prev;
Node * _next;
static int cindex;
public:
int index;
Node(void);
virtual ~Node(void);
Node *next(void); // accessor
Node *prev(void); //accessor
Node *insert(Node*);
Node*remove(void);
void splice (Node*);
};
// ------ end of definition of Node --------------------
//======================================================
//----------Start of Definition of ListNode class ------
template < class T > class List;
template<class T> class ListNode: public Node {
public:
T _val;
ListNode(T val);
friend class List<T>;
};
// ------ End of Definition of ListNode class --------------------
//====================================================++
//----------Start of Definition of List class ------
template<class T> class List {
private:
ListNode<T> *header;
ListNode<T> *win;
int _length;
public:
List(void);
~List(void);
T insert(T);
T append(T);
T prepend(T);
List * append(List*);
T remove(void);
void val(T);
T val(void);
T next(void);
T prev(void);
T first(void);
T last(void);
int length(void);
bool isFirst(void);
bool isLast(void);
bool isHead(void);
};
#endif
node.cpp
#include "header.h"
int Node::cindex=0;
Node::Node(void) :
_next(this), _prev(this)
{index=cindex++;}
Node::~Node(void) {}
Node* Node:: next(void)
{
return _next;
}
Node* Node::prev(void)
{
return _prev;
}
Node *Node::insert(Node*b){
b->_next=_next;
_next->_prev=b;
b->_prev=this;
_next=b;
return b;
}
Node*Node::remove(void)
{
_prev->_next=_next;
_next->_prev=_prev;
_next->_prev=this;
return this;
}
LstNode.cpp
#include "header.h"
template <class T> List <T> :: List(void): _length(0) //constructor for list
{
header =new ListNode<T>(NULL); //mind you this uses the LIstNode class
win=header;
}
template<class T> List <T>::~ List(void) // weird destructor
{
while (length()>0) {
first();remove();
}
delete header;
}
template <class T> T List <T> ::insert(T val)
{
win->insert( new ListNode <T> (val));
++_length;
return val;
}
template <class T> T List <T>::prepend(T val)
{
header->insert(new ListNode <T> (val));
++_length;
return val;
}
template <class T> T List <T>::append(T val)
{
header->prev()->insert(new ListNode <T> (val));
++_length;
return val;
}
template<class T> List <T>* List <T>::append(List<T>*l)
{
ListNode<T> *a =(ListNode<T>*)header->prev();
a->splice(l->header);
_length+=_length;
l->header-remove();
l->_length=0;
l->win=header;
return this;
}
template <class T> void List<T>::val(T v)
{
if (win!=header)
win->_val=v;
}
template <class T> T List<T>::val(void)
{
return win->_val;
}
template <class T> T List <T>:: next(void)
{
win=(ListNode <T>*)win->next();
return win->_val;
}
template <class T> T List <T>:: prev(void)
{
win=(ListNode <T>*)win->prev();
return win->_val;
}
template <class T> T List<T>::first(void)
{
win=(ListNode <T>*)header->next();
return win->_val;
}
template <class T> T List<T>::last(void)
{
win=(ListNode <T>*)header->prev();
return win->_val;
}
template <class T> int List <T>::length(void)
{
return _length;
}
template< class T> bool List <T> ::isFirst(void)
{
return (win==header->next()) &&(_length>0);
}
template< class T> bool List <T> ::isLast(void)
{
return (win==header->prev()) &&(_length>0);
}
template <class T> bool List <T>::isHead(void)
{
return (win == header);
}
experiment.cpp
#include <iostream>
#include "header.h"
int main()
{
List <int> dunder;
dunder.insert(9);
return 0;
}
THE PROBLEM
I first converted all of these files into object files with the generic command g++ -o file.cpp (separately) and then I did this g++ output.o output2.o ..
and this is the error I get :
experiment.o: In function `main':
experiment.cpp:(.text+0x11): undefined reference to `List<int>::List()'
experiment.cpp:(.text+0x22): undefined reference to `List<int>::insert(int)'
experiment.cpp:(.text+0x33): undefined reference to `List<int>::~List()'
experiment.cpp:(.text+0x46): undefined reference to `List<int>::~List()'
collect2: error: ld returned 1 exit status
You need to define your template class and its have its definition in the same file. As of right now template class header and definition are not separable.
Related
I'm trying to implement stack data structure using singly linked list.
This is the header file that has template class of Node and List
#pragma once
template <typename E>
class SNode {
E elem;
SNode<E>* next;
public:
friend class SLinkedList<E>;
};
template <typename E>
class SLinkedList {
private:
SNode<E>* head;
public:
SLinkedList();
~SLinkedList();
bool empty() const ();
const E& front() const;
void addFront(const E& e);
void removeFront();
};
template <typename E>
SLinkedList<E>::SLinkedList() : head(NULL) {}
template <typename E>
SLinkedList<E>::~SLinkedList() {
while(!empty()) removeFront();
}
template <typename E>
bool SLinkedList<E>::empty() const {
return head==NULL;
}
template <typename E>
const E& SLinkedList<E>::front() const {
return head->elem;
}
template <typename E>
void SLinkedList<E>::addFront(const E& e) {
SNode<E>* tmp = new SNode<E>;
tmp->elem = e;
tmp->next=head;
head=tmp;
}
template <typename E>
void SLinkedList<E>::removeFront() {
SNode<E>* old = head;
head=head->next;
delete old;
}
This is a simple main code
#include <iostream>
#include "SNode.h"
using namespace std;
int main() {
SNode<int> A(1);
SNode<int> B(2);
SLinkedList<int> L();
L.push(A);
L.push(B);
return 0;
}
When I compile this code the compiler says
SLinkedList is not a class template
I have no idea why it says that since I did put
template
You need to forward declare the SLinkedList templated class. like this:-
template <typename E>
class SLinkedList;
template <typename E>
class SNode {
E elem;
SNode<E>* next;
public:
friend class SLinkedList<E>;
};
...
There are other errors in you code. I hope you are able to resolve them by youself.
You have vexing parse with
SLinkedList<int> L();
Which declares a.. function L, taking no parameters, and returning SLinkedList<int>.
Use
SLinkedList<int> L;
or
SLinkedList<int> L{};
I've created this pretty simple dynamic list which is implemented with a template class:
Node.h
template <class T> class Node
{
public:
typedef T data_type;
typedef T& reference_type;
void setData(data_type);
void setNextNull();
void setNext(Node*);
reference_type getData();
Node* getNext();
private:
data_type data;
Node* next;
};
template <class T> void Node<T>::setData(data_type _data)
{
data=_data;
}
template <class T> void Node<T>::setNextNull()
{
next=NULL;
}
template <class T> void Node<T>::setNext(Node* _next)
{
next=_next;
}
template <class T> typename Node<T>::reference_type Node<T>::getData()
{
return data;
}
template <class T> typename Node<T>::Node* Node<T>::getNext()
{
return next;
}
List.h
#ifndef LIST_H
#define LIST_H
#include <Node.h>
template <class T> class List
{
public:
typedef Node<T> node_type;
typedef node_type* node_pointer;
typedef T data_type;
typedef T& reference_type;
List();
void push_back(data_type);
reference_type at(int);
void clear();
void swap(int,int);
int size();
private:
int list_size = 0;
node_pointer head, tail;
};
template <class T> List<T>::List()
{
head=NULL;
}
template <class T> void List<T>::push_back(data_type data)
{
if(head == NULL) {
head = new node_type;
head->setData(data);
tail = head;
} else {
node_pointer temp = new node_type;
temp->setData(data);
temp->setNextNull();
tail->setNext(temp);
tail = tail->getNext();
}
list_size++;
}
template <class T> typename List<T>::reference_type List<T>::at(int x)
{
node_pointer pointer=head;
for(int i=0; i<x; i++)
pointer=pointer->getNext();
return pointer->getData();
}
template <class T> void List<T>::clear()
{
node_pointer pointer = head;
for(int i=0; i<list_size; i++) {
node_pointer temp = pointer;
pointer=pointer->getNext();
delete(temp);
}
head=NULL;
list_size=0;
}
template <class T> void List<T>::swap(int x, int y)
{
data_type buffer=at(x);
at(x)=at(y);
at(y)=buffer;
}
template <class T> int List<T>::size()
{
return list_size;
}
#endif // LIST_H
The list works perfectly with any form of data type, except when i use a class with a parameter inside it's constructor, then I get this error:
include/Node.h error: no matching function for call to ‘Player::Player()’
What am I doing wrong??
UPDATE 1
I've added a simple constructor as suggested but I get the same error
template <class T> Node<T>::Node(data_type _data)
{
data=_data;
}
You probably haven't defined a default constructor for your Player class. Just insert an empty constructor
Player() {}
And your problem will likely to be solved.
When you write a template method and use it in the main function like this:
Node<Player>
The compiler automatically calls the constructor of the Player class.
If you didn't define any constructors in Player, the compiler will use default constructor. However, any constructor you defined will hide the default one and force you to use this one.
For instance, a constructor like
Player(string, int, int)
Prevents you to create an object like this:
Player *p = new Player();
However, if you haven't written the constructor, the piece of code above would've worked just fine.
That's why your template needs a default constructor, iff you defined a parameterized constructor.
Your class Node should have a constructor which take a T so you can construct your T by copy instead of requiring to have a default constructor and copy.
your Node class would be something like:
template <class T>
class Node
{
public:
Node(const T& data) : data(data), next(0) {}
void setNextNull();
void setNext(Node*);
const T& getData() const { return data; }
T& getData() { return data; }
Node* getNext();
private:
T data;
Node* next;
};
and so you transform
head = new node_type;
head->setData(data);
by
head = new node_type(data);
I want to create a factory, that returns AVLNode, if BinaryTree is AVLTree, and Node if the tree is not AVL. I have following code:
#include "BinaryTree.h"
#include "AVLTree.h"
class NodeFactory {
public :
template <class T>
static Node<T>* getNode(BinaryTree<T>* tree);
};
template <class T>
Node<T>* NodeFactory::getNode(BinaryTree<T>* tree) {
if (tree->isAVL()) {
return new AVLNode<T>();
} else {
return new Node<T>();
}
}
UPD: (this is BinaryTree.h)
template <class T> class Node;
template <class T> class BinaryTree {
public:
BinaryTree() {
_isAVL = false;
root = new Node<T>();
}
bool isAVL() {
return _isAVL;
}
private:
T elem;
Node<T>* root;
bool _isAVL;
};
template <class T> class Node {
public:
Node() {
left = NULL;
right = NULL;
}
T get() {
return elem;
}
void setRight(const T elem) {
right = new Node<T>();
right->set(elem);
}
void setLeft(const T elem) {
left = new Node<T>();
left->set(elem);
}
private:
T elem;
Node* left;
Node* right;
};
I removed almost all methods to make code more readable.
Now i have this error during compilation: "expected initializer before '<' token". Also Qt do not highlights in Node, but highlights in BinaryTree
It's a syntax error. You need
template <class T> // or <typename T>
Node<T>* NodeFactory::getNode(BinaryTree<T>* tree) {
template <class T>
class BTree
{
private:
T Data;
BTree* Right;
BTree* Left;
public:
BTree();
BTree(T);
~BTree();
void SetData(T);
T GetData();
void SetRight(BTree*);
BTree* GetRight();
void SetLeft(BTree*);
BTree* GetLeft();
};
Is this the way to declare a non member function template?I seriously have some doubts about these definitions.
template <class T>
BTree<T>* NewNode();
template <class T>
BTree<T>* NewNode(T);
this below does not work!! gives me an error
template <class T>
BTree<T>* NewNode()
{
BTree<T>* Node=new BTree<T>;
return Node;
}
template <class T=int>
//template <class BTree>
BTree<T>* NewNode(T Num)
{
BTree<T>* Node=new BTree<T>(Num);
return Node;
}
int main()
{
BTree<int>* Root=NULL;
Root=NewNode(1); //it says undefined reference.
Root->SetLeft(NewNode(2));
Root->SetRight(NewNode(3));
return 0;
}
The error is as the following:
1.undefined reference to `BTree<int>* NewNode<int>(int)'
2.undefined reference to `BTree<int>::SetLeft(BTree<int>*)'
3.undefined reference to `BTree<int>::SetRight(BTree<int>*)'
I'm trying to create my own template for a List class as a learning excercise. I've been having some trouble with template syntax though and i'm now getting the following error message..
main.cpp|Line 8|instantiated from here
error: template argument required for 'struct List'
In function 'int main()':
...
As far as i can tell i'm not misusing anything but this is my first time working with templates and would really appreciate someone looking through and letting me know what i'm doing wrong.
List.hpp:
#if !defined _LIST_HPP_
#define _LIST_HPP_
#include "Node.hpp"
///since we're creating a template everything must be defined in the hpp
template <typename ListType>
class List
{
public:
List();
bool Empty();
void PushFront();
void PushBack();
void PopBack();
Node<ListType>& GetHead();
private:
int _size;
Node<ListType>* _head;
Node<ListType>* _tail;
};
///implement List class here
template <typename ListType>
List<ListType>::List() : _head(0), _tail(0), _size(0)
{
}
template <typename ListType>
bool List<ListType>::Empty()
{
return _size == 0;
}
template <typename ListType>
void List<ListType>::PushFront()
{
_head = new Node<ListType>( _head , 0 );
if (!Empty())
_head->_prev->_next = _head; //set previous nodes _next to new _head
++_size;
}
template <typename ListType>
void List<ListType>::PushBack()
{
_tail = new Node<ListType>( 0 , _tail);
if (!Empty())
_tail->_next->_prev = _tail; // set old tails _prev to new tail
++_size;
}
template <typename ListType>
void List<ListType>::PopBack()
{
}
template <typename ListType>
Node<ListType>& List<ListType>::GetHead()
{
return _head;
}
#endif //define
Node.hpp:
#if !defined _NODE_HPP_
#define _NODE_HPP_
template<typename NodeType>
class Node{
public:
Node( Node* prev = 0, Node* next = 0);
void SetData(NodeType newData);
void GetData();
private:
friend class List;
NodeType _data;
Node* _next;
Node* _prev;
};
///implement Node
template <typename NodeType>
Node<NodeType>::Node(Node* prev, Node* next) : _prev(prev), _next(next)
{}
template <typename NodeType>
void Node<NodeType>::SetData(NodeType newData)
{
_data = newData;
}
template <typename NodeType>
void Node<NodeType>::GetData()
{
return _data;
}
#endif //define
Main.hpp
#include <iostream>
#include "List.hpp"
int main()
{
List<int> testl;
//test
testl.PushFront();
testl.GetHead().SetData(7); //Error thrown here??
std::cout << test1.GetHead().GetData() << std::endl;
return 0;
}
List is a class template, so you need to declare it as such in your friend declaration
template<typename ListType>
friend class List;
If you only want List<NodeType> to be a friend, you need to tell it that template argument, so then the friend declaration becomes
friend class List<NodeType>;
For this to work, it needs to know that List exists as a class template, so you need to forward-declare it at the top of Node.hpp:
template<typename ListType>
class List;