0

クラス内で使用する場合、一意の関数に渡される BinaryPredicate もクラス メンバー関数である必要があります。クラス内メンバー関数を渡そうとすると、コンパイル エラーが発生します。

cannot convert 'Solution::mycompare' from type 'bool (Solution::)(std::vector<int>, std::vector<int>)' to type 'bool (Solution::*)(std::vector<int>, std::vector<int>)'

以下はソースコードです。

class Solution {
public:
    vector<vector<int> > threeSum(vector<int> &num) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        vector<vector<int> > result;
        ...
        unique(result.begin(), result.end(), mycompare);
        return result;
    }

    bool mycompare(vector<int> v1, vector<int> v2) {
        for (int i=0; i<v1.size(); ++i) {
            if (v1[i] != v2[i])
                return false;
        }
        return true;
    }
};

この問題を解決するにはどうすればよいですか? PS: クラス外で関数を定義することはできません。

4

2 に答える 2

1

として宣言しますstatic。メンバーへのアクセスを必要としない (staticまたはしない) ため、アクセスできない理由はありません。

また、パスv1と参照v2によるconst- コピーの可能性を回避します。

于 2013-05-22T19:12:11.023 に答える
0

これを試して:

unique(result.begin(), result.end(), &mycompare);

関数に必要なアドレスuniqueを指定します。mycompare

于 2013-05-23T12:03:58.263 に答える