Salve ^^
Devo creare una lista puntata con i template e bigosna testarla con una lista int . Mi da problemi durante la compilazione. Vi incollo il programma diviso in file:
main.cpp
#include <iostream>
#include "List.h"
using std::cout;
using std::cin;
using std::endl;
int main() {
List< int > x;
int value = 4;
x.print();
x.insertFront(value);
x.print();
value=7;
x.insertFront(value);
value=2;
x.insertFront(value);
value=5;
x.insertBack(value);
x.print();
return 0;
}
ListNode.h
#ifndef LISTNODE_H_
#define LISTNODE_H_
#include "List.h"
template < class NODETYPE > class List;
template< class NODETYPE >
class ListNode {
friend class List< NODETYPE >;
public:
ListNode( NODETYPE & value ) : data(value) { Ptr=0; };
NODETYPE printData() { return data; };
private:
NODETYPE data;
ListNode< NODETYPE > * Ptr;
};
#endif /* LISTNODE_H_ */
List.h
#ifndef LIST_H_
#define LIST_H_
#include "ListNode.h"
#include <iostream>
using std::cout;
using std::endl;
template < class NODETYPE >
class List {
friend class exception;
public:
List();
~List();
void insertFront(NODETYPE & );
void insertBack(NODETYPE & );
bool removeFront( NODETYPE & );
bool removeBack( NODETYPE & );
bool isEmpty();
void print();
private:
ListNode< NODETYPE >* getNewNode( NODETYPE & );
ListNode< NODETYPE > * firstPtr;
ListNode< NODETYPE > * lastPtr;
};
template < class NODETYPE >
List< NODETYPE >::List() : firstPtr( 0 ) , lastPtr ( 0 ) {}
template < class NODETYPE >
bool List< NODETYPE >::isEmpty() {
if ( firstPtr == 0 )
return true;
return false;
}
template < class NODETYPE >
ListNode< NODETYPE > *getNewNode( NODETYPE & value ) {
ListNode< NODETYPE > * ptr = new ListNode< NODETYPE >(value);
assert( ptr != 0);
return ptr;
}
template < class NODETYPE >
void List< NODETYPE >::insertFront( NODETYPE & value ) {
if ( isEmpty() )
{
ListNode< NODETYPE > *newPtr = getNewNode(value);
firstPtr = lastPtr = newPtr;
}
else
{
ListNode< NODETYPE >* ptr = getNewNode(value);
ptr->Ptr = firstPtr;
firstPtr = ptr;
}
}
template < class NODETYPE >
void List< NODETYPE >::insertBack( NODETYPE & value ) {
if ( isEmpty() )
firstPtr = lastPtr = getNewNode(value);
else
{
ListNode< NODETYPE >* ptr = getNewNode(value);
lastPtr->Ptr = ptr;
lastPtr = ptr;
}
}
template < class NODETYPE >
bool List< NODETYPE >::removeFront( NODETYPE & value ) {
if ( isEmpty() )
return false;
else if ( firstPtr == lastPtr )
{
value = firstPtr->printData();
delete firstPtr;
delete lastPtr;
firstPtr = lastPtr = 0;
return true;
}
else
{
ListNode< NODETYPE > * ptr = firstPtr;
value = firstPtr->printData();
firstPtr = firstPtr->Ptr;
delete ptr;
return true;
}
}
template < class NODETYPE >
bool List< NODETYPE >::removeBack ( NODETYPE & value ) {
if ( isEmpty() )
return false;
else if ( firstPtr == lastPtr )
{
value = lastPtr.printData();
delete lastPtr;
delete firstPtr;
firstPtr = lastPtr = 0;
return true;
}
else
{
ListNode< NODETYPE > * ptr = firstPtr;
value = lastPtr.printData();
while ( ptr->Ptr != lastPtr )
ptr = ptr->Ptr;
lastPtr = ptr;
lastPtr->Ptr = 0;
delete ptr;
return true;
}
}
template < class NODETYPE >
void List< NODETYPE >::print() {
if ( firstPtr == 0 )
cout << "Lista non esiste "<<endl;
else
{
ListNode< NODETYPE > * currentPtr = firstPtr;
cout << "Stampa della lista dalla testa: "<<endl;
while ( currentPtr->Ptr != 0 )
{
cout << currentPtr->data << " ";
currentPtr = currentPtr->Ptr;
}
}
}
template < class NODETYPE >
List< NODETYPE >::~List() {
if ( !isEmpty() )
{
cout << "Distruzione dei nodi della lista....";
ListNode< NODETYPE > *currentPtr = firstPtr, *tempPtr;
while ( currentPtr != 0 )
{
tempPtr = currentPtr;
delete tempPtr;
currentPtr = currentPtr->Ptr;
}
cout << "Distruzione lista completata!";
}
}
Mi da errore di compilazione per quanto riguarda la funzione getNewNode(), precisamente nell righe dove voglio assegnare ad un puntatore di tipo ListNode l' indirizzo di un oggetto della classe ListNode
esdempio : ListNode< NODETYPE >* ptr = getNewNode(value); <---
l' errore è : Description Resource Path Location Type
undefined reference to `List<int>::getNewNode(int const&)' List.h
Grazie in anticipo ^^
Distinti saluti,
Alessandro