简体   繁体   English

如何在左右两侧创建运算符*(double)以进行相乘?

[英]How to create operator*(double) for multiplying on both the left and right hand side?

I'm trying to figure out how to write an overloaded operator for my Vector2d class that allows me to multiply by a scalar on both the left and right sides. 我正在试图找出如何为我的Vector2d类编写一个重载运算符,它允许我在左右两侧乘以标量。

class Vector2d
{
    double _x;
    double _y;

public:
    Vector2d(double x = 0, double y = 0) :_x(x), _y(y) {}

    Vector2d operator*(const double s) const
        { return Vector2d(_x * s, _y * s); }

    friend Vector2d operator*(const double s, const Vector2d& v);
};

Vector2d operator*(const double s, const Vector2d& v)
{
    return Vector2d(v._x * s, v._y * s);
}

If I only define the member operator*, my object can be multiplied on the right by a scalar but not the left. 如果我只定义成员运算符*,我的对象可以在右边乘以标量而不是左边。 If I add the friend function operator*, I get an error when I compile: 如果我添加了友元函数运算符*,我编译时会出错:

Vector2D.h:61: multiple definition of `Gf::operator*(double, Gf::Vector2d const&)'
Vector2D.h:61: first defined here
Vector2D.h:61: multiple definition of `Gf::operator*(double, Gf::Vector2d const&)'

What's the right way to do this? 这样做的正确方法是什么?


I was putting the operator* function in the header file. 我把operator *函数放在头文件中。 Once I moved it to the .cpp, it compiled correctly. 一旦我将它移动到.cpp,它就可以正确编译。

It looks like your file has been included multiple times, most compilers support #pragma once these days. 看起来您的文件已被多次包含,大多数编译器这些天都支持#pragma once You can also use a header guard (check for a token's definition before defining it along with the rest of your header) : 您还可以使用标头保护(在将标头与标头的其余部分一起定义之前检查标记的定义):

#ifndef VECTOR_2D
#define VECTOR_2D

class Vector2d
{
    double _x;
    double _y;

public:
    Vector2d(double x = 0, double y = 0) :_x(x), _y(y) {}

    Vector2d operator*(const double s) const
        { return Vector2d(_x * s, _y * s); }

    friend Vector2d operator*(const double s, const Vector2d& v);
};

Vector2d operator*(const double s, const Vector2d& v)
{
    return Vector2d(v._x * s, v._y * s);
}

#endif // VECTOR_2D

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

相关问题 如何防止赋值运算符左侧的临时 - How to prevent a temporary on the left hand side of the assignment operator 如何将双精度转换为字符串的总位数(小数点的左侧和右侧)? - How to convert double to string with precision for total number of digits(both left and right side of the decimal)? 如何从屏幕右侧而不是 C 中通常的左侧按 output 进行打印? - How to print by output from the right hand side of the screen instead of the usual left hand side in C? 从右手到左手如何使用substr? 如何将每8个字符从字符串转换为int? - How to use substr from right hand side to left hand side? How to convert each 8 characters from a string to an int? 操作重载 *(左侧和右侧) - operation overloading * (both left side and right side) llvm :: Module是两个运算符。*和-> *的左侧 - llvm::Module be the left side of both operator .* and ->* 如何在文本的左侧和右侧用空格填充字符数组 - How to pad char array with empty spaces on left and right hand side of the text 在示波器分辨率运算符的左侧使用“&”号? - use of ampersand on left hand side of scope resolution operator? 重载*运算符在左右两侧工作 - Overloading * operator to work on both right and left C ++重载operator =获得右手和左手过载 - C++ Overloading operator= to get right and left hand overload
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM