C ++运算符覆盖来自2个类的w /参数

时间:2012-11-04 20:03:37

标签: c++ class operator-keyword override

我是面向对象编程的新手,我想知道是否有可能为一个类创建一个运算符,该类将使用此类中的参数和另一个类,由我声明。

我必须解决的问题是给定点的线性平移。所以我创建了类PointLinearTranslation,基本上我想做的是创建一个运算符

Point operator* (Point p, LinearTranslation l) 

需要Point p,执行翻译l然后返回Point。我遇到了奇怪的错误:Point operator*(int)’ must have an argument of class or enumerated typeLinearTranslation has not been declared

它甚至可能吗?

好吧,我只发布了一点,因为它是一种分配,所以我有类Point.h

抱歉这个烂摊子,但我正在尝试一切,但这对我不起作用。

#include "LinearTranslation.h"
class Point {
public:
    int N;
    double* coordinates;

public:
    Point(int, double*);
    virtual ~Point();
    double operator[](int a);
    //friend Point operator*(LinearTranslation l);
    friend Point translation(LinearTranslation l);
};

LinearTranslation.h

#include "Point.h"

class LinearTranslation {
private:
    int N;
    double* vector;
    double** matrix;
public:
    //friend class Point;
    LTrans(int,double*,double**);
    LTrans(int);
    virtual ~LTrans();
    void write_vector();
    void write_matrix();
    LinearTranslation operator+(const LinearTranslation&);
    friend Point& translation(LTrans l, Point p);
    //friend Point& operator* (Point p, LTrans l);
};

1 个答案:

答案 0 :(得分:0)

在你的班级宣言中:

//friend Point operator*(LinearTranslation l);

friend运算符重载函数为函数提供两个参数,您只需在代码中声明一个。友元函数的最常见示例是使用重载运算符<< ,如下所示:

friend ostream& operator<<(ostream& output, const Point& p);

请注意,此函数将输出作为其第一个参数,将该点作为其第二个参数。

您的修复方法是在操作数中添加函数

friend Point operator*(LinearTranslation l, const int& MULT);