-1

next_permutationオブジェクトのベクトルで関数を使用する方法を理解するのを手伝ってください。パラメータを使用している人について読んだことがありcompますが、理解できません。オーバーロードされた演算子が問題を解決すると思っていましたが、まだエラーがスローされています。私の構文を手伝ってください、および/または関数のcompパラメータを(例で)説明してくださいnext_permutation! ありがとう!

私のメインファイルでは:

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)"

私の単純なポイントクラス:

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();
    }

};

編集:これも同じエラーをスローします:

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();
}

これも同じエラーをスローします。

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();
}
4

2 に答える 2

1

const 参照と const 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();
}

http://liveworkspace.org/code/4Drerr $0

于 2013-01-30T05:50:38.077 に答える
0

ゲッター関数は const メンバー関数である必要があります。また、double から int に変換する理由はありますか?

試す:

double x() const { return xval; }
double y() const { return yval; }
于 2013-01-30T05:53:25.297 に答える