C++ 运算符重载+;C+中的运算符+;。如何重载objecttypeA+;ObjectypeA=objectypeb?

C++ 运算符重载+;C+中的运算符+;。如何重载objecttypeA+;ObjectypeA=objectypeb?,c++,operator-overloading,C++,Operator Overloading,我有两节课。类一点,类线 一个点由一个点和两个坐标组成 类线由两个点、两个单点对象组成 如何添加两个一点,使其成为具有运算符重载的线 OnePoint a(3.0, 3.0); OnePoint b(1.0, 1.0); Line d; d = a+b; cout << d; 你的错误很小。您的Line类构造函数是多参数构造函数,您需要使用OnePoint类型的两个参数来构造Line对象 我已经更正了您的代码,您将了解如何在构造函数需要一个或多个参数时构造类的对象 f

我有两节课。类一点,类线

一个点由一个点和两个坐标组成

类线由两个点、两个单点对象组成

如何添加两个一点,使其成为具有运算符重载的线

OnePoint a(3.0, 3.0); 
OnePoint b(1.0, 1.0); 

Line d; 

d = a+b; 

cout << d;

你的错误很小。您的Line类构造函数是多参数构造函数,您需要使用
OnePoint
类型的两个参数来构造
Line
对象

我已经更正了您的代码,您将了解如何在构造函数需要一个或多个参数时构造类的对象

friend Line operator+(OnePoint a, OnePoint b) {
            Line temp(a,b);                                    // Changed here
            return temp;
        }

您还可以通过本课程学习对象构造的基本知识

您的错误非常小。您的Line类构造函数是多参数构造函数,您需要使用
OnePoint
类型的两个参数来构造
Line
对象

我已经更正了您的代码,您将了解如何在构造函数需要一个或多个参数时构造类的对象

friend Line operator+(OnePoint a, OnePoint b) {
            Line temp(a,b);                                    // Changed here
            return temp;
        }

您还可以通过本课程学习对象构造的基本知识

您的错误非常小。您的Line类构造函数是多参数构造函数,您需要使用
OnePoint
类型的两个参数来构造
Line
对象

我已经更正了您的代码,您将了解如何在构造函数需要一个或多个参数时构造类的对象

friend Line operator+(OnePoint a, OnePoint b) {
            Line temp(a,b);                                    // Changed here
            return temp;
        }

您还可以通过本课程学习对象构造的基本知识

您的错误非常小。您的Line类构造函数是多参数构造函数,您需要使用
OnePoint
类型的两个参数来构造
Line
对象

我已经更正了您的代码,您将了解如何在构造函数需要一个或多个参数时构造类的对象

friend Line operator+(OnePoint a, OnePoint b) {
            Line temp(a,b);                                    // Changed here
            return temp;
        }
您还可以通过本教程学习有关对象构造的基本知识

您可以(在C++11中)重写
操作符+
之类的内容

Line operator+(const OnePoint& lhs, const OnePoint& rhs)
{
    return {lhs, rhs};
}
您可以重写(在C++11中)您的
操作符+

Line operator+(const OnePoint& lhs, const OnePoint& rhs)
{
    return {lhs, rhs};
}
您可以重写(在C++11中)您的
操作符+

Line operator+(const OnePoint& lhs, const OnePoint& rhs)
{
    return {lhs, rhs};
}
您可以重写(在C++11中)您的
操作符+

Line operator+(const OnePoint& lhs, const OnePoint& rhs)
{
    return {lhs, rhs};
}

你所需要做的一切——不做任何改变——就是

OnePoint a(3.0, 3.0);
OnePoint b(1.0, 1.0); 
Line d(a,b);
如果你真的想做

d = a + b;
然后需要提供一个
运算符+()
,该运算符接受两个类型为
OnePoint
的参数,并返回一行
。替代方案是作为
OnePoint

 // definition of Line here

class OnePoint
{
    public:
        Line operator+(const OnePoint &) const;

         //  other member functions, etc 
};

Line OnePoint::operator+(const OnePoint &rhs) const
{
     Line retval(*this, rhs);
     return retval;
}
或作为非成员

 // definitions of Line and OnePoint here

 Line operator+(const OnePoint &lhs, const OnePoint &rhs);   // declaration, not definition

 Line operator+(const OnePoint &lhs, const OnePoint &rhs)
 {
      Line retval(lhs, rhs);
      return retval;
 }
显然,在上述两种情况下,我都假设
操作符+()
可以根据需要访问(例如,访问
”构造函数)


请注意,从数学上讲,您所做的是向后的。不使用
a+b
语法添加点以获得一条线-一条线使用一对点表示,而不是求和。相反,向量被添加到点上以生成其他点。

您所需要做的就是在不改变任何内容的情况下

OnePoint a(3.0, 3.0);
OnePoint b(1.0, 1.0); 
Line d(a,b);
如果你真的想做

d = a + b;
然后需要提供一个
运算符+()
,该运算符接受两个类型为
OnePoint
的参数,并返回一行
。替代方案是作为
OnePoint

 // definition of Line here

class OnePoint
{
    public:
        Line operator+(const OnePoint &) const;

         //  other member functions, etc 
};

Line OnePoint::operator+(const OnePoint &rhs) const
{
     Line retval(*this, rhs);
     return retval;
}
或作为非成员

 // definitions of Line and OnePoint here

 Line operator+(const OnePoint &lhs, const OnePoint &rhs);   // declaration, not definition

 Line operator+(const OnePoint &lhs, const OnePoint &rhs)
 {
      Line retval(lhs, rhs);
      return retval;
 }
显然,在上述两种情况下,我都假设
操作符+()
可以根据需要访问(例如,访问
”构造函数)


请注意,从数学上讲,您所做的是向后的。不使用
a+b
语法添加点以获得一条线-一条线使用一对点表示,而不是求和。相反,向量被添加到点上以生成其他点。

您所需要做的就是在不改变任何内容的情况下

OnePoint a(3.0, 3.0);
OnePoint b(1.0, 1.0); 
Line d(a,b);
如果你真的想做

d = a + b;
然后需要提供一个
运算符+()
,该运算符接受两个类型为
OnePoint
的参数,并返回一行
。替代方案是作为
OnePoint

 // definition of Line here

class OnePoint
{
    public:
        Line operator+(const OnePoint &) const;

         //  other member functions, etc 
};

Line OnePoint::operator+(const OnePoint &rhs) const
{
     Line retval(*this, rhs);
     return retval;
}
或作为非成员

 // definitions of Line and OnePoint here

 Line operator+(const OnePoint &lhs, const OnePoint &rhs);   // declaration, not definition

 Line operator+(const OnePoint &lhs, const OnePoint &rhs)
 {
      Line retval(lhs, rhs);
      return retval;
 }
显然,在上述两种情况下,我都假设
操作符+()
可以根据需要访问(例如,访问
”构造函数)


请注意,从数学上讲,您所做的是向后的。不使用
a+b
语法添加点以获得一条线-一条线使用一对点表示,而不是求和。相反,向量被添加到点上以生成其他点。

您所需要做的就是在不改变任何内容的情况下

OnePoint a(3.0, 3.0);
OnePoint b(1.0, 1.0); 
Line d(a,b);
如果你真的想做

d = a + b;
然后需要提供一个
运算符+()
,该运算符接受两个类型为
OnePoint
的参数,并返回一行
。替代方案是作为
OnePoint

 // definition of Line here

class OnePoint
{
    public:
        Line operator+(const OnePoint &) const;

         //  other member functions, etc 
};

Line OnePoint::operator+(const OnePoint &rhs) const
{
     Line retval(*this, rhs);
     return retval;
}
或作为非成员

 // definitions of Line and OnePoint here

 Line operator+(const OnePoint &lhs, const OnePoint &rhs);   // declaration, not definition

 Line operator+(const OnePoint &lhs, const OnePoint &rhs)
 {
      Line retval(lhs, rhs);
      return retval;
 }
显然,在上述两种情况下,我都假设
操作符+()
可以根据需要访问(例如,访问
”构造函数)


请注意,从数学上讲,您所做的是向后的。不使用
a+b
语法添加点以获得一条线-一条线使用一对点表示,而不是求和。相反,向量被添加到点以生成其他点。

不要覆盖
+
。这是出人意料的

如果确实需要干净的语法,请使用命名运算符

这是一条12行:


我认为这比a+b更清楚。(是的,命名操作符库有点混乱,但使用点的代码非常清楚)

不要覆盖
+
来执行此操作。这是出人意料的

如果确实需要干净的语法,请使用命名运算符

这是一条12行:

我认为这比a+b更清楚。(是的,命名操作符库有点混乱,但使用点的代码非常清楚)

不要