C++ 模板函数以C++;

C++ 模板函数以C++;,c++,function,templates,overloading,C++,Function,Templates,Overloading,我希望能够返回两个值中的较大值,无论是整数、双精度还是类。我已重载了要使用的类的比较运算符。函数在主目录中给我链接器错误。代码如下: template <typename tType> void returnGreater(tType &A, tType &B) { if(A > B) { cout << "A is greater"; //testing return A; } els

我希望能够返回两个值中的较大值,无论是整数、双精度还是类。我已重载了要使用的类的比较运算符。函数在主目录中给我链接器错误。代码如下:

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;
    }
}
模板
无效返回值更大(t类型A、t类型B)
{
如果(A>B)
{

不能改变返回类型

template <typename tType>
tType& returnGreater(tType &A, tType &B)
^^^^^^ void is not the right return type for what you want to do.
您可以通过使用显式类型来实现这一点:

int j = returnGreater<int const>(i, 20); // OK.
intj=returnGreater(i,20);//好的。
检查这个

#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;

}
#包括
使用名称空间std;
模板
t类型和返回值更大(t类型和A、t类型和B){
如果(A>B)
{
cout a>>b;

coutCall。可能吗?不是重复的。这涉及到模板及其实际实现,而不是它们的实现位置……链接器错误是?谢谢。但它仍然不起作用。我遇到了链接器错误,当我尝试传递整数时,我得到了匹配的函数调用errors@VectorCroc,将有助于诊断问题
#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;

}