0

I want to be able to return the greater of two values, whether it it an integer, double, or class. I have overloaded the comparison operator for the class I want to use. The function is giving me linker errors in the main. Here is the code:

template <typename tType>
void returnGreater(tType &A, tType &B)
{
    if(A > B)
    {
        cout << "A is greater"; //testing
        return A;
    }
    else
    {
        cout << "B is greater"; //testing
        return B;
    }
}

2 Answers2

2

Change the return type.

template <typename tType>
tType& returnGreater(tType &A, tType &B)
^^^^^^ void is not the right return type for what you want to do.

PS The above function won't work when you have a mix of const and non-const arguments. You'll have to figure out how to resolve that issue.

For example, you can't use:

int i = 10;
int j = returnGreater(i, 20);

You can make that work by using explicit types:

int j = returnGreater<int const>(i, 20); // OK.
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • thanks. It still is not working though. I am getting a linker error, and when I try to pass integers, I get matching function call errors – Vector Croc Apr 02 '15 at 05:34
  • 1
    @VectorCroc, an [MCVE](http://stackoverflow.com/help/mcve) will be helpful in diagnosing the problem. – R Sahu Apr 02 '15 at 05:39
0

Check this....

#include <iostream>

using namespace std;

template <typename tType>
tType& returnGreater(tType &A, tType &B){

    if(A > B)
    {
        cout << "A is greater"; //testing
        return A;
    }
    else
    {
        cout << "B is greater"; //testing
        return B;
    }
}

int main(){

    int a,b;
    cin>>a>>b;
    cout<< returnGreater(a,b)<<endl;
    return 0;

}

Your return type is not correct.

Mukit09
  • 2,956
  • 3
  • 24
  • 44