List using Templates

Discuss all kind of algorithms and data structures from their mathematical and programming sides.

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

List using Templates

Postby Planet_EN » Wed Dec 07, 2005 8:40 am

Can anyone please tell me whats wrong with this code, why is it not executing???

My Compiler says:
Error: Unresolved external 'List<int>::List<int>(List<int>&)' referrenced from h:\data structures\myfile.obj

and if i remove comments from the commented part then i get like almost 6 more errors, can anyone please please help me out on this.............

[syntax="cpp"]
#include<iostream>
using namespace std;

template<class T>
class List{

protected:
struct Node
{
T _data;
Node *_next;
Node(T data = T(), Node* next = NULL):
_data(data),_next(next) { }
};

Node *Head, *Current;
List ( List & value );

public:
List():Head(new Node),Current(Head)
{ }

List( const List& );



void insert( const T& value )
{
Node* P = new Node( value , Current->_next );

if( P == NULL )
std::cerr << "Out of Space";
else
{
Current->_next = P;
Current = Current->_next;
}

}

void First( )
{
if( Head->_next != NULL )
Current = Head->_next;
}

int operator !() const
{
return Current != NULL;
}

List operator ++()
{
if( Current != NULL )
Current = Current->_next;
return *this;
}

T & operator ( ) ( ) const
{
if( Current != NULL )
return Current->_data;
else
return Head->_data;
}

/*
***********Problematic code**************

const List opeator = ( List & value )
{
for ( value.First( ); !value; value++ )
{
Current->_next = new Node( Value( ) , Curent->_next );
Current = Current->_next;
}

Current->_next = 0;
First();
value.First();
return *this;
}*/
int isEmpty() const { return Head->_next == NULL;}

};

template<class T>
void Print ( List<T> &L )
{
if( L.isEmpty() == NULL )
cout << "Empty List";
else
for( L.First(); !L; ++L )
cout << L( ) << ends;
}


int main()
{
List<int> b;
b.insert(5);
Print(b);
return 0;
}
[/syntax]
Planet_EN
 
Posts: 18
Joined: Mon Mar 07, 2005 8:27 pm

Postby Darryl » Wed Dec 07, 2005 9:13 am

1. you spelled operator wrong in: const List opeator = ( List & value )
2. you have 2 different copy constructor declarations(1 public, 1 protected) but no copy constructor definitions. Commententing those out allows compilation, however the default copy constructors are insufficient for copying pointers properly.

Implement your copy constructor(s) and all will be ok
User avatar
Darryl
 
Posts: 1342
Joined: Wed Sep 01, 2004 10:50 am
Location: Cayman Islands

Postby Planet_EN » Wed Dec 07, 2005 9:36 am

Darryl wrote:1. you spelled operator wrong in: const List opeator = ( List & value )
2. you have 2 different copy constructor declarations(1 public, 1 protected) but no copy constructor definitions. Commententing those out allows compilation, however the default copy constructors are insufficient for copying pointers properly.

Implement your copy constructor(s) and all will be ok


thank you its working now but somehow, it's displaying "Empty List" at its execution.

Here's the amended code:
[syntax="cpp"]
#include<iostream>
using namespace std;

template<class T>
class List{

protected:
struct Node
{
T _data;
Node *_next;
Node(T data = T(), Node* next = NULL):
_data(data),_next(next) { }
};

Node *Head, *Current;

public:
List():Head(new Node),Current(Head)
{ }

List( const List& );



void insert( const T& value )
{
Node* P = new Node( value , Current->_next );

if( P == NULL )
std::cerr << "Out of Space";
else
{
Current->_next = P;
Current = Current->_next;
}

}

void First( )
{
if( Head->_next != NULL )
Current = Head->_next;
}

int operator !() const
{
return Current != NULL;
}

List operator ++()
{
if( Current != NULL )
Current = Current->_next;
return *this;
}

T & operator ( ) ( ) const
{
if( Current != NULL )
return Current->_data;
else
return Head->_data;
}

void Print ( const List<T>& L )
{
if( L.Head->_next == NULL )
cout << "Empty List";
else
for( L.First(); !L; ++L )
cout << L( ) << ends;

}

const List operator = ( List & value )
{
for ( value.First( ); !value; value++ )
{
Current->_next = new Noe( Value( ) , Curent->_next );
Current = Current->_next;
}

Current->_next = 0;
First();
value.First();
return *this;
}
int isEmpty() const { return Head->_next == NULL;}

};

template<class T>
List<T>::List(const List<T>& L)
{
Head = L.Head;
Current = L.Current;
}

template<class T>
void Print ( List<T> &L )
{
if( L.isEmpty() == NULL )
cout << "Empty List";
else
for( L.First(); !L; ++L )
cout << L( ) << ends;
}


int main()
{
List<int> b;
b.insert(5);
b.insert(11);
Print(b);
return 0;
}
[/syntax]
Planet_EN
 
Posts: 18
Joined: Mon Mar 07, 2005 8:27 pm

Postby Darryl » Wed Dec 07, 2005 4:02 pm

Code: Select all
int isEmpty() const {  return Head->_next == NULL;} //bad
if( L.isEmpty() == NULL ) // really bad:  (0 == 0) == true;


you really should use boolean types here;
Code: Select all
bool isEmpty() const {  return Head->_next == NULL;}//correct return
if( L.isEmpty() ) // or L.isEmpty() == true)// corect comparison


plus it reads clearer using booleans
User avatar
Darryl
 
Posts: 1342
Joined: Wed Sep 01, 2004 10:50 am
Location: Cayman Islands


Return to Algorithms & Data Structures

Who is online

Users browsing this forum: No registered users and 0 guests