C++ C+中最大值的运算符重载+;

C++ C+中最大值的运算符重载+;,c++,class,operators,C++,Class,Operators,我正在用算术运算符(+、-、*、/)和std::max重载实现一个新的变量类型(NewType)。虽然算术运算符可以工作,但max函数存在一些问题。有人能告诉我我缺少什么吗 #include <iostream> using namespace std; class NewType { private: float val; public: NewType(float v) { val = v; } // Arithmetic operators

我正在用算术运算符(+、-、*、/)和std::max重载实现一个新的变量类型(NewType)。虽然算术运算符可以工作,但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;
}
#包括
使用名称空间std;
类新类型{
私人:
浮动增值税;
公众:
NewType(float v){val=v;}
//算术运算符
friend NewType运算符+(常数NewType&c1,常数NewType&c2);
友元新类型运算符-(常数新类型&c1,常数新类型&c2);
友元新类型运算符*(常数新类型&c1,常数新类型&c2);
友元新类型运算符/(常数新类型&c1,常数新类型&c2);
float GetVal(){return val;}
float max(const NewType&lhs,const NewType&rhs){返回lhs.val>rhs.val?lhs.val:rhs.val;}
};
//算术运算
NewType操作符+(constnewtype&c1,constnewtype&c2){返回NewType(c1.val+c2.val);}
NewType操作符-(const NewType&c1,const NewType&c2){返回NewType(c1.val-c2.val);}
NewType运算符*(constnewtype&c1,constnewtype&c2){返回NewType(c1.val*c2.val);}
NewType操作符/(constnewtype&c1,constnewtype&c2){返回NewType(c1.val/c2.val);}
int main(){
新a型=10.2;
新b型=8.4;
新类型c=a+b;

cout您已经将
max
实现为一个非静态成员函数。您可以这样调用它:

NewType a(0.0f), b(1.0f), c(2.0f);
NewType d = a.max(b, c);
请注意,此操作实际上根本不需要
a
,除非您将
max
声明为非静态成员函数。一种解决方案是将其设置为非成员函数

// outside of the class body
float max(const NewType &lhs, const NewType &rhs) {
    return lhs.GetVal() > rhs.GetVal() ? lhs.GetVal() : rhs.GetVal();
}

另一个(更好的,imo)解决方案是重载
操作符
friend float max(…
您调用的是
std::max
,而不是类中定义的
max
std::max
使用
运算符可能是退出使用此运算符的时候了:
使用名称空间std;
@drescherjm始终是删除
使用名称空间std;
的时候了。声明max为好友,并在类之外定义max,就像以前一样对于运算符重载,您当前的实现调用std::max非常感谢您的回答。根据您所说的将max放在外部效果很好。但是另一个想法似乎不起作用(重载包括类NewType{private:float val;public:NewType(float v){val=v;}布尔operator@smjee:注意,
std::max
返回传递给它的相同类型(这是很自然的事情),因此如果传递2个
NewType
对象,它将返回一个
NewType
对象。而max函数返回一个浮点值。输出
operator@smjee:嗯,在我等待的时候,另一个问题是您重载了
运算符库!解决了问题:)
// member version, inside the class
bool operator<(const NewType& rhs) const {
    return val < rhs.val;
}

// non-member version, outside the class
bool operator<(const NewType& lhs, const NewType& rhs) {
    return lhs.GetVal() < rhs.GetVal();
}
float GetVal() const { return val; }