C++ 使用重载运算符返回错误结果的比较函数

C++ 使用重载运算符返回错误结果的比较函数,c++,templates,C++,Templates,我不太确定我的错误在哪里,而且我对编程非常陌生,我知道我违反了很多标准化规则。我在学习。用户输入两个整数并进行比较,它总是报告输入的第一个整数更大,即使它不应该更大。我想说我的getlength方法有问题,但现在我不太确定 #include "stdafx.h" #include <iostream> using namespace std; template <class T> //Function to find larger value a = firs

我不太确定我的错误在哪里,而且我对编程非常陌生,我知道我违反了很多标准化规则。我在学习。用户输入两个整数并进行比较,它总是报告输入的第一个整数更大,即使它不应该更大。我想说我的
getlength
方法有问题,但现在我不太确定

#include "stdafx.h"
#include <iostream>




using namespace std;

template <class T>

//Function to find larger value a = first number, b = second number
T findLargerValue(T a, T b) {

    return (a>b ? a:b);
}

//Class trip

template <class T>
class Trip {


public: 
     Trip<T>::Trip();

    Trip(T a){

         length = a;

    }

    T operator>(Trip yourTrip);


    T getLength() {

        return length;
    }

private:
    T length;

};

// Template function to carry into 

/** This is where I seem to be losing my logic, I am trying to push larger of the two objects to the screen but it is only  displaying the first user input This is supposed to take in the two bojects created and measure the lengths.**/

template<class T> 
T Trip<T>::operator>(Trip yourTrip) {
    if (length > yourTrip.getLength()) {

        return length;
    }

    else{
        return yourTrip.getLength();
    }
}

template<class T>
Trip<T>::Trip()
{

}



int main()
{

    // input for integer values

    int num1 , num2 ;

    cout << "Enter integer value 1: ";
    cin >> num1;

    cout << endl << endl;

    cout << "Enter integer value 2: ";
    cin >> num2;



    //display message of first two numbers
    cout <<"Frist Number was: " << num1 << " Second Number was: " << num2 << "\nThe Larger number of the two is "
        <<  findLargerValue(num1, num2) << endl << endl;


    //input for double values

    double num3, num4;

    cout << "Enter double value 1: ";
    cin >> num3;

    cout << endl << endl;

    cout << "Enter double value 2: ";
    cin >> num4;

    //display mesasge of second two numbers
    cout << "Frist Number was: " << num3 << " Second Number was: " << num4 << "\nThe Larger number of the two is "
         << findLargerValue(num3, num4) << endl << endl;

    // Display message for Trip function/objects 

    int fTlength, sTlength;
    cout << "How many miles was your first trip: ";
    cin >> fTlength;

    Trip<int> fTrip(fTlength);

    cout << endl;

    cout << "How many miles was your second trip: ";
    cin >> sTlength;

    Trip<int> sTrip(sTlength);

    Trip<int> tripLonger;
    tripLonger = findLargerValue(fTrip, sTrip);

    cout << "First trip in miles: " << fTlength << endl << "Second Trip in miles: " << sTlength << endl;



    cout << "The longest trip was " << tripLonger.getLength() << " miles." << endl;



    system("pause");

    return 0;
}
#包括“stdafx.h”
#包括
使用名称空间std;
模板
//用于查找较大值的函数a=第一个数字,b=第二个数字
T findLargerValue(T a,T b){
返回(a>b?a:b);
}
//集体旅行
模板
集体旅行{
公众:
Trip::Trip();
行程(TA){
长度=a;
}
T操作员>(跳闸您的跳闸);
T getLength(){
返回长度;
}
私人:
T长度;
};
//要执行的模板函数
/**这就是我似乎失去逻辑的地方,我试图将两个对象中较大的一个推到屏幕上,但它只显示第一个用户输入,这应该是在创建的两个对象中获取并测量长度**/
模板
T跳闸::操作员>(跳闸您的跳闸){
如果(长度>yourTrip.getLength()){
返回长度;
}
否则{
返回您的行程。getLength();
}
}
模板
Trip::Trip()
{
}
int main()
{
//整数值的输入
int num1,num2;
cout>num1;

您可能错误地重载了
运算符>
。它应该返回
false的
true
,但(在本例中)返回一个整数值。它应该如下所示:

template<class T> 
bool Trip<T>::operator>(const Trip& yourTrip)
{
    return length > yourTrip.length;
}
模板
bool跳闸::操作员>(恒定跳闸和您的跳闸)
{
返回长度>yourTrip.length;
}

比较运算符应指示比较是否为真。在您的情况下,它返回的是
length
yourTrip.length
。这是
findLargerValue
为两个对象调用
运算符>
时出现的问题。它会检查结果是否为真,并且因为任何整数都不是
0
true
,它几乎总是返回第一个对象。如果您输入了负数和
0
,它就会返回第二个对象。

tripLonger=findLargerValue(fTrip,sTrip);
这行谢谢!您一指出,我就花了5分钟来解决这个问题!