2012-06-27 25 views
0

我想添加兩個對象,它們在同一個類中。運算符重載+添加兩個對象

在類的私有部分,我有兩個int變量

class One { 

private: 
int num1, num2; 
public: 

    One operator+=(const One&); // - a member operator that adds another One object - to the current object and returns a copy of the current object 
    friend bool operator==(const One&, const One&); // - a friend operator that compares two One class objects for equality 
}; 

One operator+(const One&, const One&);// - a non-friend helper operator that adds One objects without changing their values and returns a copy of the resulting One 

我不知道我有一個問題在opeartor+我猜

One operator+(const One &a, const One &b){ 

One c,d,r; 

c = a; 
d = b; 

r += b; 
r += a; 

return r; 
} 

我覺得上面的代碼錯誤,但我試圖使用像b.num1和我得到編譯錯誤

error: 'int One::num1' is private error: within this context

我也不能使用b-> num1,因爲上述函數不在成員函數部分。

error: base operand of '->' has non-pointer type 'const One' 

這是它在main

Result = LeftObject + RightObject;

+0

Whenever'operator +'錯了嗎?順便說一句:你忘了初始化成員。 –

+0

@ user956030對不起? – Ali

+0

你不能做'b.num1',''num1'是私人會員。如果你需要訪問它,你需要有一個* getter *函數。由於您尚未創建指針類型,因此'b->'無效。你打算添加私人整數? –

回答

4

如果你已經實現了這個成員函數:

One One::operator+=(const One&); 

然後就可以實現這樣的非成員的加法運算符:

One operator+(const One& lhs, const One& rhs) { 
    One result = lhs; 
    result += rhs; 
    return result; 
} 

這可以稍微簡化成以下:

One operator+(One lhs, const One& rhs) { 
    return lhs += rhs; 
} 

此模式(其可以爲所有操作者/運營商適應 - 分配對)將操作員分配版本聲明爲成員 - 它可以訪問私有成員。它將操作員版本聲明爲非朋友非成員 - 這允許在操作員的任何一方進行類型提升。

除了:該+=方法應該返回一個參考*this,而不是一個副本。所以它的聲明應該是:One& operator+(const One&)


EDIT:一個工作示例程序如下。

#include <iostream> 
class One { 
private: 
    int num1, num2; 

public: 
    One(int num1, int num2) : num1(num1), num2(num2) {} 
    One& operator += (const One&); 
    friend bool operator==(const One&, const One&); 
    friend std::ostream& operator<<(std::ostream&, const One&); 
}; 

std::ostream& 
operator<<(std::ostream& os, const One& rhs) { 
    return os << "(" << rhs.num1 << "@" << rhs.num2 << ")"; 
} 

One& One::operator+=(const One& rhs) { 
    num1 += rhs.num1; 
    num2 += rhs.num2; 
    return *this; 
} 

One operator+(One lhs, const One &rhs) 
{ 
    return lhs+=rhs; 
} 

int main() { 
    One x(1,2), z(3,4); 
    std::cout << x << " + " << z << " => " << (x+z) << "\n"; 
} 
+0

我不認爲你應該修改lhs –

+1

我不這樣做。在這兩種情況下,我修改lhs的*本地副本*。首先,我明確地複製副本。在第二種情況下,我要求編譯器創建一個副本(因爲'lhs'是按值傳遞的,不是通過引用傳遞的。) –

+0

它可以工作,但是我的結果仍然錯誤,可能我的另一個函數不完整。 – Ali

1

怎麼叫我不明白爲什麼operator+是錯誤的:

#include <stdio.h> 

class One { 
    public: 
     One(int n1, int n2): num1(n1),num2(n2) {} 

    private: 
     int num1, num2; 
    public: 
     One operator+=(const One& o) { 
      num1 += o.num1; 
      num2 += o.num2; 
      return *this; 
     } 
     friend bool operator==(const One&, const One&); // - a friend operator that compares two One class objects for equality 
     void print() { 
      printf("%d,%d\n", num1, num2); 
     } 
}; 

One operator+(const One& a, const One& b) { 
    One r(0,0); 
    r += b; 
    r += a; 
    return r; 
} 

int main() { 
    One a(1,2),b(3,4); 
    One r = a + b; 
    r.print(); 
} 
+0

通常複製一個參數,然後添加另一個參數(即'One r = a; r + = b;')。即使類型沒有添加標識,這也可以工作。 –

+1

'#include '也許? :) –

+0

@Mike_Seymour這是不尋常的,但沒有錯 –