I'm trying to fix the error on the line "Felix.append( Felix );" by ajusting the function and peoviding a template specialization like the compiler told me. I'm copying my template specialization from an article on this website, why is my template specialization for the the append function not working? Thanks.
#include <iostream>
template <class T>
class List
{
private:
public:
List():head(0),tail(0),theCount(0) {std::cout <<"List constructor called.\n";}
virtual~List(){std::cout<<"List destructor called.\n";}
void Insert( int value );
int is_present( int value ) const;
int is_empty() const { return head == 0; }
int count() const { return theCount; }
private:
class ListCell
{
public:
ListCell(int value, ListCell *cell = 0):val(value),next(cell){}
int val;
ListCell *next;
};
ListCell *head;
ListCell *tail;
int theCount;
};
template <>
class List <T>
{
public:
void List <T>::append( int value );
};
class Cat
{
private:
public:
Cat():head(0),tail(0),theCount(0) {std::cout <<"Cat constructor called.\n";}
virtual~Cat(){std::cout<<"Cat destructor called.\n";}
void Insert( int value );
void append( int value );
int is_present( int value ) const;
int is_empty() const { return head == 0; }
int count() const { return theCount; }
private:
class ListCell
{
public:
ListCell(int value, ListCell *cell = 0):val(value),next(cell){}
int val;
ListCell *next;
};
ListCell *head;
ListCell *tail;
int theCount;
};
int main()
{
List <Cat> Felix;
Felix.append( Felix );
std::cout <<"Felix is " <<
( Cat_List.is_present( Felix ) ) ? """not " << "present.\n";
}
Your List template doesn't use the template argument. I suspect that the ints at lines 11, 12, 19, 20, and 26 should be Ts instead.
You've defined a class Cat that is a list of integers. Why is a cat a list of integers? What do the integers mean? I strongly suspect that this isn't your intention.
Fix the template. Then figure out what class Cat should really contain.
Line 29 to 34 doesn't make sense. Remove it. Instead put appen(...) in the public interface of your template class List like so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
template <class T>
class List
{
private:
public:
List():head(0),tail(0),theCount(0) {std::cout <<"List constructor called.\n";}
virtual~List(){std::cout<<"List destructor called.\n";}
void Insert( int value ); // Replace int with const T &
int is_present( int value ) const; // Replace int with const T &
int is_empty() const { return head == 0; }
int count() const { return theCount; }
void append( const T &value ) // How is this different from Insert()?
{
...
}
...
Remove all the List stuff from your class Cat. It shall not know anything about the list. Instead e.g. add a string for the name etc.