Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 理解C++;对于对象的向量_C++_Parameters_Operator Overloading_Permutation - Fatal编程技术网

C++ 理解C++;对于对象的向量

C++ 理解C++;对于对象的向量,c++,parameters,operator-overloading,permutation,C++,Parameters,Operator Overloading,Permutation,我想知道如何在对象向量上使用next\u permutation函数。我读过关于使用comp参数的人的文章,但我不理解。我原以为重载运算符可以解决问题,但我仍然会遇到抛出的错误。请帮助我学习语法和/或解释(举例说明)next_permutation函数的comp参数!谢谢 在我的主文件中: vector<Point> source; //fill vector with Points, say 4 of them (1,2)(2,3)(3,4)(4,5) next_permuta

我想知道如何在对象向量上使用
next\u permutation
函数。我读过关于使用
comp
参数的人的文章,但我不理解。我原以为重载运算符可以解决问题,但我仍然会遇到抛出的错误。请帮助我学习语法和/或解释(举例说明)
next_permutation
函数的
comp
参数!谢谢

在我的主文件中:

vector<Point> source;

//fill vector with Points, say 4 of them (1,2)(2,3)(3,4)(4,5)

next_permutation(source.begin(), source.end()); // at run I get error "Invalid operands to binary expression ('Const Point' and 'Const Point)"
矢量源;
//用点填充向量,比如说4个点(1,2)(2,3)(3,4)(4,5)
下一个置换(source.begin(),source.end());//在运行时,我收到错误“二进制表达式('Const Point'和'Const Point'的操作数无效)”
我的简单要点课程:

class Point {
private:
    double xval, yval;
public:
    Point(int x = 0, int y = 0) {
        xval = x;
        yval = y;
    }

    int x() { return xval; }
    int y() { return yval; }

    friend bool operator<(Point& lhs, Point& rhs){
        return lhs.x() < rhs.x() || (lhs.x()==rhs.x() && lhs.y()<rhs.y()) ;
    }

    friend bool operator==(Point& lhs, Point& rhs) {
        return lhs.x()==rhs.x() && lhs.y()==rhs.y();
    }

};
类点{
私人:
双xval,yval;
公众:
点(整数x=0,整数y=0){
xval=x;
yval=y;
}
int x(){return xval;}
int y(){return yval;}

friend bool运算符使用常量引用和常量getter函数

int x() const { return xval; }
int y() const { return yval; }

friend bool operator<(const Point& lhs, const Point& rhs){
    return lhs.x() < rhs.x() || (lhs.x()==rhs.x() && lhs.y()<rhs.y()) ;
}

friend bool operator==(const Point& lhs, const Point& rhs) {
    return lhs.x()==rhs.x() && lhs.y()==rhs.y();
}
intx()常量{return xval;}
int y()常量{return yval;}

friend bool操作符Getter函数应该是const成员函数,还有什么原因可以将double转换为int

尝试:


比较运算符不应该被允许修改对象,也不应该成为不必要的朋友。谢谢@chris的提示,但我不知道如何执行它。如果我去掉“friend”并将.x()更改为.xval和.y()对于.yval,我得到了错误。如果我只有一个参数,我仍然会得到错误。你能写出更好的语法让我遵循吗?使getters常量,使用它们,使比较运算符的
点和
参数常量,并删除
友元
。我已经这样做了:
int x()常量{return xval;}
int y()const{return yval;}
布尔运算符的操作方式大致相同,谢谢@ForEveR!这允许下一个置换按预期工作,但我必须更改.x()和.y()相应地转换为.xval和.yval。@ruya:我们应该找出原因,因为你不应该也不想这样做。我没有理由从double转换为int!哈哈,谢谢你的关注。
int x() const { return xval; }
int y() const { return yval; }

bool operator<(const Point& lhs){
    return lhs.x() < x() || (lhs.x()==x() && lhs.y()<y()) ;
}

bool operator==(const Point& lhs) {
    return lhs.x()==x() && lhs.y()==y();
}
int x() const { return xval; }
int y() const { return yval; }

friend bool operator<(const Point& lhs, const Point& rhs){
    return lhs.x() < rhs.x() || (lhs.x()==rhs.x() && lhs.y()<rhs.y()) ;
}

friend bool operator==(const Point& lhs, const Point& rhs) {
    return lhs.x()==rhs.x() && lhs.y()==rhs.y();
}
double x() const { return xval; }
double y() const { return yval; }