I need help understanding what it is I need to do in debugging this code for my class. I have corrected what I can, but I am running into an issue with running a templated function with an object and moving the funtion return value to a character variable.
I am getting this error on line 77 and line 84 of my code.
error C2440: '=' : cannot convert from 'Animal' to 'char' 1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
line 77 -- ans = askType <Animal> (lion);
line 84 -- ans = askType <Vegetable> (carrot);
I believe I need to overload the = operator to solve this problem, but I haven't quite figured out overloading the = operator. I would like to understand this, so any help will be greatly appreciated.
You've defined a template function that assumes (and can only work with) char types. However, the return type is defined by how you instantiate the template (since it returns an object of type T).
You're telling the function to return an Animal object when you write askType<Animal>(lion), but the function is attempting to return a char instead (and fails to do so)
You don't need the function to be templated, since all it does is prompt the user for a char and return it.
I see what you are saying, but not using the template causes another error C2664 that the funtion can not convert the parameter from variable type Animal to char.
There isn't a way to use an overloaded operator to pass a char value based on a function using an object parameter? I don't see it working without using a template since I am dealing with two different data types.
Okay, so I ran and compiled your code and noticed several important errors:
1. You're treating askType as an object all of a sudden - which it's not. IF you had an askType objectaskType.lion would access a member called 'lion'.
If you're trying to pass lion as a parameter, what you did before was correct. askType<Animal>(lion);
z. The way you've written it right now, if you tell the program that the carrot is an animal, you will be told "Good!".
3. You don't really need a template function for what you're doing. The reason why is that the function doesn't really DO anything with the Animal or Vegetable's information, outside of accessing its name. If anything, you could simply pass Animal.name or Vegetable.name to an ordinary function that accepted a string as a parameter.
(nitpicking a bit now)
4. You don't really need to overload the << operator to output the name; a simple accessor function to return the name would suffice
eg cout << x.GetName() << endl;
Hope this helps.
(I had to remove .h from <iostream> and <string>, as well as include <cstring> to compile this, btw. Just letting you know that while your compiler may accept those headers, its best to just use the non .h versions, which are deprecated. Conio.h doesn't have an updated version, to the best of my knowledge)
That code I posted in the second post was not my rewrite. That was the original my instructor gave me to debug. My first post contains the revisions I think need to be made.
I see what you are saying on the asktype function. To me it seems like a waste because we are leaning templates this week, and I don't see the value in debugging this code if there isn't much to learn around functions.
The code compiles and I think it should work the way it was intented. I'll see what king of grade I'll get. I just hope I am learning something.