Why can't I show the string>

My book is asking me to debug some template code which is utter gibberish. I'm not including the original file but instead my attempt to turn it in to legible code.

Please help me one step at a time.

Why can,t I show the string from the template classs object?

Please show me how to do it.

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

template <class List>
class Cat
{
public:
//Cat append(std::string myString) {theString=myString;}
Cat append(Cat<int>&);
Cat is_present(Cat<int>&);
Cat show(){return theString;} //error
private:
std::string theString;
};

int main()
{
Cat <int> Felix;
Felix.append(Felix);
std::cout <<"This is the string : " << Felix.show() << std::endl; //error
//std::cout<<"Felix is" << ( Felix.is_present( Felix ) ) ? "": "not " << "present\n";
}

THis was the code that the book wants me to debug:
1
2
3
4
List<Cat> Cat_List;
Cat Felix;
CatList.append( Felix );
cout << "Felix is " << ( Cat_List.is_present( Felix ) ? "": "not " << "present\n";

can anyone at least try and inform me what the author wants me to do with this code to make it into a working program? thanks.
closed account (1vRz3TCk)
AL88 wrote:
I'm not including the original file but instead my attempt to turn it in to legible code.

Post the original code in full...unless the above four lines is it.
Thats it thats all the book shows and asks you to debug it. Can you explain what the author is trying to get me to do with this code?
Thanks.
closed account (1vRz3TCk)
If there is no more information about the types or what the error is, I guess he is asking you to look for errors...get used to noticing small syntax errors or missing parenthesis, say on line 4.
The error msg for th code from the first post is quite clear.
Error	C2679	binary '<<': no operator found which takes a right-hand operand
of type 'Cat<int>' (or there is no acceptable conversion) main.cpp(20)


The operator << doesn't know how to output a Cat. So you need to overload the << operator for the Cat class.
Can someone show me how to make a function that takes a string for appaend in this template set up please?

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

class List
{
public:
private:
    
};

template <class T>
class Cat
{
public:
std::string append(std::string theString){theString=myString;}
private:
std::string myString;    
};

int main()
{
Cat <List> Felix;
Felix.append( Felix );

}

//std::cout<<"Felix is " <<
//( Cat_List.is_present( Felix ) ) ? "": "not " << "present\n"; 
I never overloaded an operator with a template before can someone show me the correct syntax of how to do it? Thanks.
Topic archived. No new replies allowed.