简体   繁体   English

VS2010中的运算符重载和LNK2019错误

[英]Operator Overloading and LNK2019 error in VS2010

I'm writing a Point class which holds three floats, xyz, some functions and overloaded operators. 我正在编写一个Point类,其中包含三个浮点数,xyz,一些函数和重载的运算符。 I coded operators in the following form: 我以以下形式编码运算符:

inline Point Point::operator+ (Point point)
{
    return Point(x + point.x, y + point.y, z + point.z);
}

inline void Point::operator+= (Point point)
{
   x += point.x;
   y += point.y;
   z += point.z;
}

Is this the right way to overload these operators? 这是使这些运算符过载的正确方法吗? I've tested it and it works but I've seen another form like so: 我已经对其进行了测试,但是可以看到另一种形式:

inline Point& Point::operator+ (Point& point)
{
    return Point(x + point.x, y + point.y, z + point.z);
}

inline Point& Point::operator+= (Point& point)
{
    x += point.x;
    y += point.y;
    z += point.z;
        return *this;
}

What is the difference between the two forms? 两种形式有什么区别?

Also I can use the operators inside my Point.cpp file but if I try to use it say in Main.cpp, I get a lnk2019 error for unresolved external symbol. 我也可以在Point.cpp文件中使用运算符,但是如果尝试在Main.cpp中使用它,则对于未解析的外部符号,我会收到lnk2019错误。 Oddly, my functions work outside the defining file. 奇怪的是,我的函数在定义文件之外工作。 What am I missing to get these operators to work outside the file they're defined in? 我缺少让这些运算符在定义它们的文件之外工作的东西吗?

The first operator should be 第一个运算符应该是

inline Point Point::operator+ (const Point &point) const
{
    return Point(x + point.x, y + point.y, z + point.z);
}

it shouldn't return a reference because its excepted to construct a new object and not modify a existing one (a general rule of thumb). 它不应返回引用,因为它可以构造一个新对象并且不修改现有对象(一般的经验法则)。 const is added to the function because the point its called one shouldn't be modified. 将const添加到函数中是因为不应修改其被调用的点。 a reference to the point as argument is added so there won't be unnecessary copy 添加了对该点的引用作为参数,因此不会有不必要的复制

The second one should be 第二个应该是

inline Point& Point::operator+= (const Point& point)
{
    x += point.x;
    y += point.y;
    z += point.z;
    return *this;
}

because it supposed to modify an existing point, so it should return a reference. 因为它应该修改现有的点,所以它应该返回一个引用。 const is added to the argument because it shouldn't be changed. 将const添加到参数中,因为它不应更改。 the function itself is not const because it should modify the point itself. 函数本身不是const,因为它应该修改点本身。

The linker error you get is because of the inline. 您收到的链接器错误是由于内联。 either provide full implementation in header file, or remove the inline. 在头文件中提供完整的实现,或删除内联。

inline Point& Point::operator+ (Point& point)
{
    return Point(x + point.x, y + point.y, z + point.z);
}

That's wrong, its returning a reference to a temporary that no longer exists when the function returns. 这是错误的,它返回对临时函数的引用,该临时函数在返回时不再存在。 It would result in a segfault if you are lucky enough. 如果您足够幸运,将导致段错误。 Most compilers will give you a warning if you try to do it. 如果尝试这样做,大多数编译器都会给您警告。 Ideally you would write this operator as a free function, implemented in terms of your member operator+= . 理想情况下,您可以将此操作符编写为自由函数,并根据您的成员operator+=

inline Point& Point::operator+= (Point& point)
{
    x += point.x;
    y += point.y;
    z += point.z;
        return *this;
}

This is pretty much the preferred way, except that you should be taking the point as a const reference, otherwise you cannot use it with temporaries. 这是相当多的首选方式,但你应该考虑的point作为一个const引用,否则你无法使用的临时使用。 Returning a reference to itself is what allows chaining operators. 返回对自身的引用允许链接运算符。 It's generally said that when in doubt, do as the ints, and the ints do that. 一般说来,如果有疑问,请照常做,然后才做。

Summing up, the 'cannonical' implementation would be: 总结起来,“规范的”实现是:

inline Point& Point::operator+=( Point const& point )
{
    x += point.x;
    y += point.y;
    z += point.z;
    return *this;
}

inline Point const operator+( Point left, Point const& right )
{
     return left += right;
}

Note that the having operator+ be a free function allows conversions to Point to happen at both operands and not just the right one. 请注意,让operator+为自由函数允许在两个操作数(不仅是正确的操作数)上进行到Point转换。 It's conveniently implemented in terms of operator+= : The left argument is taken by value in order to have a temporary copy that we can modify, and we do so by adding the right argument to it. 它可以根据operator+=方便地实现:左参数由值采用,以具有我们可以修改的临时副本,我们可以通过向其添加右参数来实现。 Since operator+= returns a reference, we use such return in order to provide the value that would be copied as the function result. 由于operator+=返回一个引用,因此我们使用这种返回来提供将被复制为函数结果的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM