算術演算子(+、 - 、*、/)とstd :: maxがオーバーロードされた新しい変数型(NewType)を実装しています。算術演算子は動作しますが、max関数には問題があります。誰かが私が紛失していることを指摘してくれますか?C++でmaxの演算子オーバーロード
#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;
}
'友人フロートMAX(...' –
あなたはない 'max'があなたのクラスで定義され、STD :: max''呼んでいる。 'のstd :: max'は'演算子< 'デフォルトで使用しています – Cameron
これを使用して終了する時間はたぶん: 'using namespace std;' – drescherjm