2012-11-04 20 views
1

我是新來的面向對象編程,我想知道是否可能爲一個類創建一個運算符,該類將使用來自此類的參數和另一個由我聲明的參數。C++操作符重寫2個類的參數

我必須解決的問題是給定點上的線性轉換。所以,我創建的類PointLinearTranslation,基本上就是我想要做的就是創建一個運營商

Point operator* (Point p, LinearTranslation l) 

這將需要Point p,做翻譯升,然後返回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

請張貼一些代碼。 –

+3

聽起來好像你在聲明LinearTransform之前試圖聲明這個函數。另外,請張貼代碼。 – CrazyCasta

+0

好的,我編輯了我的文章,請幫助我!我被困住了...... – MartiMarti

回答

0

在你的類聲明:

//friend Point operator*(LinearTranslation l); 

一個friend運算符重載函數有參數的功能,你只申報一個在你的代碼。朋友函數的最常見的例子是使用重載操作< <像這樣:

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

注意,該函數取輸出作爲它的第一個參數,和點作爲它的第二個參數。

你的解決將是你的操作添加到函數

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