New to templates

Write your question here.
Can you please tell me what I'm doing wrong here?
#include <iostream>
using namespace std;

template <class T>
T max (T x, T y)
string result;
if (x > y)
cout << x << " is bigger than " << y;
else if (x == y)
cout << x << " is equal to " << y;
cout << y << " is bigger than " << x;

int main()
double x = 5.6, y = 4.4;
max (x, y);
Last edited on
Your function says it returns an object of type "T", so where is the return statement to do that?
Also, don't call your function max.

There already are functions named std::max, and by writing using namespace std; , if any of them are visible to the compiler (especially the one that takes two double parameters), it won't know which function you mean.

Your code is a perfect demonstration of why we say don't write using namespace std;

Last edited on
Thanks. Actually I edited this code a lot and that's why it's messy.
Thanks Repeater)
Registered users can post here. Sign in or register to post.