How can I make the if else shortcut compile?

How do I need to re-write the if else shortcut on line 51 to make it 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( std::string value );
    void append ( std::string value );
    void is_present(){std::string value;}
    int is_empty() const { return head == 0; }
    int count() const { return theCount; }
private:
    class ListCell
    {
    public:
        ListCell(std::string 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" );
Felix.is_present ("Felix")  ? std::cout << "is present.\n" : std::cout << "is not present.\n";
}
Line 14 may look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool is_present(std::string value) const // Why to you pass the type std::string? I would expect the type T
{
  bool is_ok = false;

  for(ListCell *cur = head; cur; cur = cur->next)
  {
    if(value == cur->val)
    {
      is_ok = true;
      break;
    }
  }
  return is_ok;
}



std::cout << (Felix.is_present ("Felix") ? "is present.\n" : "is not present.\n");
that didnt compile untill I changed the line 14 called type to T, now I get an error on line 64. How do I fix that?

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
#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( std::string value );
    void append ( std::string value );
    bool is_present(T value) const // Why to you pass the type std::string? I would expect the type T
{
  bool is_ok = false;

  for(ListCell *cur = head; cur; cur = cur->next)
  {
    if(value == cur->val)
    {
      is_ok = true;
      break;
    }
  }
  return is_ok;
}
    int is_empty() const { return head == 0; }
    int count() const { return theCount; }
private:
    class ListCell
    {
    public:
        ListCell(std::string 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_present ("Felix") ? "is present.\n" : "is not present.\n");
}
Last edited on
You "get an error". Psychic are we? If yes, then you are too. Here: "an answer". You are welcome.

It would be common courtesy for you to show the error message.

Sure, we can compile your code and get some error message, but we might use different compiler. You should first learn to read the messages that your compiler writes. We cannot help in that, for we can't see what your system says.


For this time:
You have a function:
bool List<Cat>::is_present( Cat ) const;
You call it with
"Felix"
What is the effective type of constant string literal? Is it const char*?

How do you convert const char* into a Cat?
Last edited on
Can anyone help me get a const char* or std::string on line 66? 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
#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";}
    T append ( const char* );
    void Insert( const char* value );
    bool is_present(const char* value) const // Why to you pass the type std::string? I would expect the type T
{
  bool is_ok = false;

  for(ListCell *cur = head; cur; cur = cur->next)
  {
    if(value == cur->val)
    {
      is_ok = true;
      break;
    }
  }
  return is_ok;
}
    int is_empty() const { return head == 0; }
    int count() const { return theCount; }
private:
    class ListCell
    {
    public:
        ListCell(const char* value, ListCell *cell = 0):val(value),next(cell){}
        const char * val;
        ListCell *next;
    };
    
    ListCell *head;
    ListCell *tail;
    int theCount;
};


template <>
class List <class T>
{
public:
void List<T>::append ( char const*);
};



class Cat
{
    Cat(char const*){}
    public:
    //Cat(const char&[]){}
    private:
};
  
      
int main()
{
List <Cat> Felix;
Felix.append( "Felix" );
std::cout << (Felix.is_present ("Felix") ? "is present.\n" : "is not present.\n");
}
Can anyone help me get a const char* or std::string on line 66?

What do you mean by "get"?

Perhaps you should tackle linked lists, templates, and cats as separate issues.
This one has no cats:
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
#include <iostream>
#include <string>

template <class T>
class List
{
public:
    List() {}
    ~List()
    {
        while ( head ) {
            auto next = head->next;
            delete head;
            head = next;
        }
    }
    void prepend( T value )
    {
        head = new ListCell( value, head );
    }
    bool is_present(const char* value) const
    {
        for ( ListCell *cur = head; cur; cur = cur->next) {
            if(value == cur->val) return true;
        }
        return false;
    }
private:
    struct ListCell
    {
        ListCell( T value, ListCell *cell = nullptr ) : val(value), next(cell) {}
        T val;
        ListCell* next;
    };
    
    ListCell* head {nullptr};
};
      
int main()
{
    List<std::string> snafu;
    snafu.prepend( "Felix" );
    std::cout << (snafu.is_present("Felix") ? "is present.\n" : "is not present.\n");
}
Are you sure your problem is solved?

It might compile but it's far from working....
Topic archived. No new replies allowed.