Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使用模板函数减去两个对象?_C++_Templates_Operator Overloading - Fatal编程技术网

C++ 如何使用模板函数减去两个对象?

C++ 如何使用模板函数减去两个对象?,c++,templates,operator-overloading,C++,Templates,Operator Overloading,假设我有: template <typename T> T resta(T a, T b) { return(a-b); } 我主要有: int main(int argc, char *argv[]) { int a = 5, b=3; cout << "Ejemplo de plantilla es:\t" << resta(a, b) <<endl; double d1 = 232.23, d2 =

假设我有:

template <typename T>

T resta(T a, T b) {

    return(a-b);
}
我主要有:

int main(int argc, char *argv[]) {

    int a = 5, b=3;

    cout << "Ejemplo de plantilla es:\t" << resta(a, b) <<endl;

    double d1 = 232.23, d2 = 231.4;

    cout << "Ejemplo de plantilla es:\t" << resta(d1, d2) <<endl;

    Person per1(40), per2(35);

    cout << "Ejemplo de plantilla es:\t" << resta(per1, per2) <<endl;  //IT FAILS HERE

}
intmain(intargc,char*argv[]){
INTA=5,b=3;

cout您将无法获得任何内容的
模板
函数
resta()
。您已经:

friend int operator-(Person a, Person b);
您可以通过以下方式获得:

Person a; //...
Person b; //...
int diff = a - b;

这可能正是您想要的。

您将无法获得任何内容的
模板
函数
resta()
。您已经:

friend int operator-(Person a, Person b);
您可以通过以下方式获得:

Person a; //...
Person b; //...
int diff = a - b;

这可能正是您想要的。

您将无法获得任何内容的
模板
函数
resta()
。您已经:

friend int operator-(Person a, Person b);
您可以通过以下方式获得:

Person a; //...
Person b; //...
int diff = a - b;

这可能正是您想要的。

您将无法获得任何内容的
模板
函数
resta()
。您已经:

friend int operator-(Person a, Person b);
您可以通过以下方式获得:

Person a; //...
Person b; //...
int diff = a - b;
这可能正是您想要的。

在C++11中

template<class T> auto resta(T a, T b) -> decltype(a-b)
{
     return a-b;
}
template auto resta(ta,tb)->decltype(a-b)
{
返回a-b;
}
在C++14中

template <class T> auto resta(T a, T b)
{
  return a - b;
}
模板自动重启(TA、TB)
{
返回a-b;
}
在C++11中

template<class T> auto resta(T a, T b) -> decltype(a-b)
{
     return a-b;
}
template auto resta(ta,tb)->decltype(a-b)
{
返回a-b;
}
在C++14中

template <class T> auto resta(T a, T b)
{
  return a - b;
}
模板自动重启(TA、TB)
{
返回a-b;
}
在C++11中

template<class T> auto resta(T a, T b) -> decltype(a-b)
{
     return a-b;
}
template auto resta(ta,tb)->decltype(a-b)
{
返回a-b;
}
在C++14中

template <class T> auto resta(T a, T b)
{
  return a - b;
}
模板自动重启(TA、TB)
{
返回a-b;
}
在C++11中

template<class T> auto resta(T a, T b) -> decltype(a-b)
{
     return a-b;
}
template auto resta(ta,tb)->decltype(a-b)
{
返回a-b;
}
在C++14中

template <class T> auto resta(T a, T b)
{
  return a - b;
}
模板自动重启(TA、TB)
{
返回a-b;
}

需要将班级人员更改为

class Person {

    public:

    int age;

    Person(int ag) {
        age = ag;
    }

   friend int resta(Person a, Person b);
    int operator-(Person b) {
        int result = resta(this->age, b.age);
        return result;
    }


    //friend resta(a, b);

};
总的来说,只需调用重载运算符

cout << "Ejemplo de plantilla es:\t" << (per1 - per2) <<endl;  

cout需要将班级人员更改为

class Person {

    public:

    int age;

    Person(int ag) {
        age = ag;
    }

   friend int resta(Person a, Person b);
    int operator-(Person b) {
        int result = resta(this->age, b.age);
        return result;
    }


    //friend resta(a, b);

};
总的来说,只需调用重载运算符

cout << "Ejemplo de plantilla es:\t" << (per1 - per2) <<endl;  

cout需要将班级人员更改为

class Person {

    public:

    int age;

    Person(int ag) {
        age = ag;
    }

   friend int resta(Person a, Person b);
    int operator-(Person b) {
        int result = resta(this->age, b.age);
        return result;
    }


    //friend resta(a, b);

};
总的来说,只需调用重载运算符

cout << "Ejemplo de plantilla es:\t" << (per1 - per2) <<endl;  

cout需要将班级人员更改为

class Person {

    public:

    int age;

    Person(int ag) {
        age = ag;
    }

   friend int resta(Person a, Person b);
    int operator-(Person b) {
        int result = resta(this->age, b.age);
        return result;
    }


    //friend resta(a, b);

};
总的来说,只需调用重载运算符

cout << "Ejemplo de plantilla es:\t" << (per1 - per2) <<endl;  

肯定不能减法…肯定减法…肯定减法…肯定减法…在C++14中你甚至可以省略
->decltype(a-b)
。在C++14中你甚至可以省略
->decltype(a-b)
。在C++14中你甚至可以省略
->decltype(a-b)
。在C++14中你甚至可以省略
->->decltype(a-b)
。不,我想使用通用模板减去任何内容(双精度、整数、人员、未来类等)不,我想使用通用模板减去任何内容(双精度、整数、人员、未来类等)不,我想使用通用模板减去任何内容(双精度、整数、人员、未来类等)不,我想使用通用模板减去任何内容(double、int、Persons、future类等)