超载为最大我实现一个新的变量类型(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