How do I get the List constructor to work for line 47?
I tried writing it out as the compiler suggested it but it then asked me to make a template specialization for it and its not letting me have two template specializations or move it in to the 1st specialization. What do I do? What is the way forward? Thanks
#include <iostream>
template <class T>
class List
{
private:
public:
List<T>(T):head(0),tail(0),theCount(0) {std::cout <<"List constructor called.\n";}
virtual~List(){std::cout<<"List destructor called.\n";}
void Insert( T value );
int is_present( T value ) const;
int is_empty() const { return head == 0; }
int count() const { return theCount; }
private:
class ListCell
{
public:
ListCell(T value, ListCell *cell = 0):val(value),next(cell){}
T val;
ListCell *next;
};
ListCell *head;
ListCell *tail;
T theCount;
};
template <>
class List <class T>
{
public:
void List <T>::append( int value );
};
class Cat
{
public:
private:
};
int main()
{
List <Cat> Felix;
Felix List::append( Felix );
std::cout <<"Felix is " <<
( Cat_List.is_present( Felix ) ) ? """not " << "present.\n";
}
At line 9, you've declared your constructor to take an argument of type T. You haven't declared or defined a default constructor, and the compiler won't automatically create one for you.
At line 47, you're trying to invoke a non-existant default constructor.
Either supply an argument to the constructor when you declare the variable, or else define a default constructor.
Ok thats fixed. How do I get line 50 to compile? Iv'e tried putting the function to take a string into the class, the compiler tells me to make a template specialization for the function then it doesn't recognize it. What do I do to get line 50 to compile?
do i need to overload << to get the text to work? I never overloaded before, is it true when overloading you use 2 types 1 for the function itself and one for the string being taken or returned?
my guess is he wants me to check with : ? if felix has been input to the object then use that test to print not present or present if the input is there. Does that sound right to you?
There are multiple error (at least three) so the best way to approach this problem is probably to first understand what the program is trying to do and then change the code so that it do so correctly without compilation errors.