0

Why is this happening ?

Could you please run this code and tell me why the result is.. you will see the result , an error ! What I did wrong and how can I make it work ? The code : #include <iostream> #include <string> using namespace std; template <typename Type> class MyClass { public: Type sum(Type a, Type b) { return a+b; } }; template <> class MyClass<string> { public: string Afiseaza(string a) { cout<<a; } }; int main() { MyClass<float> *obj; cout<<obj->sum(2.2,3.2)<<endl; MyClass<string> obj1; cout<<obj1.Afiseaza("Ceva")<<endl; }

17th Jun 2020, 5:28 PM
Ovidiu Călin
Ovidiu Călin - avatar
3 Respostas
+ 1
Well, let's see… 1. You have a class with a template declaration but no template cast type, intresting. 2. That class has a public method that has a string return type but returns nothing, plot thickens. 2.5. That function calls the stdout buffer and then sends itself to stdout buffer. (~_~¡) What you need to do is add a return statement to the function Afifseaza of templated class MyClass<string>. Make sure it returns a string class object.
17th Jun 2020, 6:03 PM
Anubhav Mattoo
Anubhav Mattoo - avatar
+ 1
Oh, thanks a lot it worked like a charm im still new =)
17th Jun 2020, 6:26 PM
Ovidiu Călin
Ovidiu Călin - avatar
+ 1
I have made that mistake a lot of times.
17th Jun 2020, 6:32 PM
Anubhav Mattoo
Anubhav Mattoo - avatar