why is my template specialization not working?

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  #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";
}
Last edited on
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.
please can you show me how to fix the template argument? Thanks.
ok i fixed that by using class T
can you tell me what you think the Cat class should 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.
Topic archived. No new replies allowed.