How do I get the List constructor to work for line 47?

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

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
  #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";
}
It seems like you intended for List<T>::theCount to be an integer, not of type T (which may or may not be an integer).
fixing that still doesn't let me declare the object. How do I make declaring the object compile?
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.

Last edited on
the compiler warning on the constructor says 1 argument expected 0 provided
Isn't that what I just said?

You've defined a constructor with one argument.

You're trying to invoke a default constructor, i.e. one with 0 arguments.
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?

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
#include <iostream>
#include <string>

template <class T>
class List
{
private:
public:
    List(){}
    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 );
    void append ( std::string 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;
    int theCount;
};


template <>
class List <class T>
{
public:
   
void List <T>::append( std::string );
};

class Cat
{
    public:
    private:
};
  
      
int main()
{
List <Cat> Felix;
Felix.append( Felix );
std::cout <<"Felix is " <<
( Cat_List.is_present( Felix ) ) ? "" "not " << "present.\n";
}
Ok thats fixed. How do I get line 50 to compile?

You pass a string into the function, just like you promised the compiler you would (lines 13, 37).
ok thats fixed, the book is asking me to debug this code, have you any idea what the author is trying to get me to do with lines 51 and 52

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
#include <iostream>
#include <string>

template <class T>
class List
{
private:
public:
    List(){}
    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 );
    void append ( std::string value );
    void is_present( std::string 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;
    int theCount;
};


template <>
class List <class T>
{
public:
   
void List <T>::append( std::string );
};

class Cat
{
    public:
    private:
};
  
      
int main()
{
List <Cat> Felix;
Felix.append( "Felix" );
std::cout <<"Felix is " <<
( Felix.is_present( "Felix" ) ) ? "":  ""not " << "present.\n";
} 
whats he trying to do with "?"
That's the ternary operator:

https://en.wikipedia.org/wiki/%3F:

EDIT: Although those quote marks in the final part look messed up.
Last edited on
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?
No, it's already defined for C-type string literals.

But, as I said, the quote-marks seem to be messed up, so you may need to fix that.
can you show me an overload example of << in the context of this program?
I've already told you, no overloading is needed here.
so do i just need to fix the if else statement with : and ?
?
Yes. I've already told you what I think the problem is.
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?
That's correct, yes.

Specifically, the words
Felix is 
are always output, and the word
present
is always output. The conditional bit is the bit between those two strings.
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.
Topic archived. No new replies allowed.