超載爲最大我實現一個新的變量類型(NEWTYPE)與算術運算符(+, - ,*,/)和std ::最大過載。雖然算術運算符工作,但最大函數有一些問題。有人能提供一些我失蹤的指針嗎?操作員在C++
#include <iostream>
using namespace std;
class NewType {
private:
float val;
public:
NewType(float v) { val = v; }
// Arithmetic operators
friend NewType operator+(const NewType &c1, const NewType &c2);
friend NewType operator-(const NewType &c1, const NewType &c2);
friend NewType operator*(const NewType &c1, const NewType &c2);
friend NewType operator/(const NewType &c1, const NewType &c2);
float GetVal() { return val; }
float max(const NewType &lhs, const NewType &rhs) { return lhs.val > rhs.val ? lhs.val : rhs.val; }
};
// Arithmetic Operations
NewType operator+(const NewType &c1, const NewType &c2) { return NewType(c1.val + c2.val); }
NewType operator-(const NewType &c1, const NewType &c2) { return NewType(c1.val - c2.val); }
NewType operator*(const NewType &c1, const NewType &c2) { return NewType(c1.val * c2.val); }
NewType operator/(const NewType &c1, const NewType &c2) { return NewType(c1.val/c2.val); }
int main() {
NewType a = 10.2;
NewType b = 8.4;
NewType c = a+b;
cout << c.GetVal() << std::endl;
NewType d = max(a,b);
cout << d.GetVal() << endl;
return 0;
}
'朋友FLOAT MAX(...' –
你打電話'的std :: max',在你的類中定義不是'max'。'的std :: max'使用'運營商<'默認,你還沒有實現 – Cameron
也許它的時間使用該退出:'使用命名空間std;' – drescherjm