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; }
3 Réponses
+ 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.
+ 1
Oh, thanks a lot it worked like a charm im still new =)
+ 1
I have made that mistake a lot of times.