Hi, I have some problems with pointers in C++

Hi , I have just start programing and for sure I should start simple but this is my program, I have to create a generic linked list and to inherit to a stack , this is my program:


#ifndef DATA_H
#define DATA_H
#include <iostream>




template <class T>
class Data
{
public:

Data(){}

Data(Data& newData){value = newData.value;}

Data(T newValue){value = newValue;}

void setValue(T newValue){value = newValue;}

T getValue(){return value;}

virtual ~Data(){};

void print(){std::cout << value << std::endl; }


private:
T value;
};








#ifndef NODE_H
#define NODE_H
#include <iostream>
#include "Data.h"



template <class T>
class Node
{
//friend class List;
public:

Node();

Node<T>(T& nodeObject);

~Node<T>();

void setNodeData(T newValue);

void printNodeData();




Node* returnNextPtr(Node* ptr)
{
return ptr->next;
}



void setNextPtr(Node*ptr)
{
next = ptr;
}



T* returnObjPtr()
{
return objPtr;
}



private:
T* objPtr;
Node* next;

};


template <class T>
Node<T>::Node()
{
objPtr = new T();
next = 0;
}


template <class T>
Node<T>::Node(T& nodeObject)
{
objPtr = new T(nodeObject);
next = 0;
}


template <class T>
Node<T>::~Node()

{
if (objPtr!= 0){
//HERE i HAVE THE PROBLEM , WHEN i DELETE THE OBJECT OF THE NODE
// IF i COMMENT OUT THIS IT COMPILES WITH NO ERRORS

delete objPtr;

}
}





template <class T>
void Node<T>::setNodeData(T newValue)
{
objPtr->setValue(newValue);
}




template <class T>
void Node<T>::printNodeData()
{
std::cout << objPtr->getValue();
std::cout << "\n";
}






#ifndef LIST_H
#define LIST_H
#include "Node.h"


template <class T>
class List
{
public:
List<T>();

bool isEmpty();

void printList();

virtual~List<T>();

void insertFirst(const T& newItem);

void deleteNode();

int getNoOfNodes()
{
return noNodes;
}


private:
T* first;
int noNodes;

};




template <class T>
List<T>::List()
{
first = 0;
noNodes = 0;
}


template <class T>
bool List<T>::isEmpty()
{
return (first==0);
}


template <class T>
void List<T>::printList()
{
T* current;
current = first;
while(current != 0)
{
current->printNodeData();
current = current->returnNextPtr(current);
}

}



template <class T>
void List<T>::deleteNode(){


T* temp = first;
first = first->returnNextPtr(first);
delete temp;
noNodes--;

}




template <class T>
void List<T>::insertFirst(const T& newItem)
{

T* newNode = new T(newItem);
newNode->setNextPtr(first);
first = newNode;
noNodes++;


}



template <class T>
List<T>::~List()
{
T* temp ;

if(!isEmpty()){


while(first != 0){
temp = first;
first = first->returnNextPtr(first);

delete temp;temp = 0;
noNodes--;

}

}
}



#ifndef STACK_H
#define STACK_H
#include "List.h"



template <class T>
class Stack:public List<T>
{

typedef List<T> baseClass;
using baseClass::deleteNode;


public:
void push(T& inputNode){this->insertFirst(inputNode);}
void pop(){deleteNode();}


};


I have problems with destructors, I got an infinite loop, please ignore cout statement, this is my beginner way to find problems
Last edited on
Why would you define the class functions outside the class, especially when you are using templates...

Also space and format your code:
[code]code here<-[/code]
Thank you for your time ,
Topic archived. No new replies allowed.