c++ C++ 中函数指针的错误 C2064模板

发布于 2024-11-27 18:38:45 字数 2847 浏览 1 评论 0原文

如果我编译下面的代码,我会得到一个

“错误 C2064:术语不计算为采用 2 个参数的函数”

doOpp() 方法中出现 。如果我将 q = f(p, i); 更改为 q = add(p, i); 一切都可以正常编译(使用 add() 是 doOpp() 方法中的第二个参数)。我尝试了几种解决方案,但似乎没有任何效果。

template <class T = double>
class Points {
public: 
    Points(void);
    Points(Point3d<T> *, int);
    Points(T *, int);
    Points(Points &);
    ~Points(void);

    void clear();
    int size();
    void push_back(Point3d<T> &);
    typename vector<Point3d<T>>::iterator begin(void){return pnts.begin();};
    typename vector<Point3d<T>>::iterator end(void){return pnts.end();};

    void assign(Point3d<T> *, int);
    void assign(T *, int);
    void assign(Points &);

    Points valid(void);
    Points valid(int *);
    bool setValid(int, bool);
    int validSize();

// Pointer to operator function prototype
    typedef Point3d<T> (__thiscall Points<T>::*oppFunc) (Points<T> &, int);

    Points operator=(Points &);

// + Operator method
    Points operator+(Points &p){return doOpp(p, &Points<T>::add);};
    Points operator-(Points &){return doOpp(p, &Points<T>::sub);};
    Points operator*(Points &){return doOpp(p, &Points<T>::mlt);};;

private:

    Points(int);
    void assign(int);
    int assign(Points &, Points &);
    vector<Point3d<T>> pnts;
    bool isSameSize(Points<T> &, char *);

// General purpose operator function
    Points doOpp(Points<T> &, oppFunc);

// Addition method called by doOpp
    Point3d<T> add(Points &p, int i) {return pnts[i] + p.pnts[i];}
    Point3d<T> sub(Points &p, int i) {return pnts[i] - p.pnts[i];}
    Point3d<T> mlt(Points &p, int i) {return pnts[i] * p.pnts[i];}
};

template <class T> int
Points<T>::assign(Points &p1, Points &p2) {
    int s = min(p1.size(), p2.size());
    assign(s);

    return s;
}

template <class T> bool
Points<T>::isSameSize(Points &p, char *s) {
    bool    same = (pnts.size() == p.pnts.size());

    #ifndef POINTS_NO_THROW
    if (!same) {
        PointsException e;
        e.setMessage((string) s + " source sizes not equal");
        throw e;
    }
    #endif

    return same;
}

template <class T> Points<T>
Points<T>::doOpp(Points &p, oppFunc f) {
    isSameSize(p, __FUNCTION__);
    Points<T>   r;
    int         s = r.assign(*this, p);
    Point3d<T>  q;

    for (int i = 0; i < s; i++) {
        q = f(p, i); // Causes error C2064 but q = add(p, i); works fine
    }

    return r;
}


int
main () {
    Points<>    p, q;

    q.assign(a, 4);
    p = q + q;

    return 1;
}

If I compile the code below, I get an

"Error C2064: term does not evaluate to a function taking 2 arguments"

in the doOpp() method. If I change q = f(p, i); for q = add(p, i); everything compiles fine (with add() being the second argument in the doOpp() method). I've tried a couple of solutions but nothing seems to work out.

template <class T = double>
class Points {
public: 
    Points(void);
    Points(Point3d<T> *, int);
    Points(T *, int);
    Points(Points &);
    ~Points(void);

    void clear();
    int size();
    void push_back(Point3d<T> &);
    typename vector<Point3d<T>>::iterator begin(void){return pnts.begin();};
    typename vector<Point3d<T>>::iterator end(void){return pnts.end();};

    void assign(Point3d<T> *, int);
    void assign(T *, int);
    void assign(Points &);

    Points valid(void);
    Points valid(int *);
    bool setValid(int, bool);
    int validSize();

// Pointer to operator function prototype
    typedef Point3d<T> (__thiscall Points<T>::*oppFunc) (Points<T> &, int);

    Points operator=(Points &);

// + Operator method
    Points operator+(Points &p){return doOpp(p, &Points<T>::add);};
    Points operator-(Points &){return doOpp(p, &Points<T>::sub);};
    Points operator*(Points &){return doOpp(p, &Points<T>::mlt);};;

private:

    Points(int);
    void assign(int);
    int assign(Points &, Points &);
    vector<Point3d<T>> pnts;
    bool isSameSize(Points<T> &, char *);

// General purpose operator function
    Points doOpp(Points<T> &, oppFunc);

// Addition method called by doOpp
    Point3d<T> add(Points &p, int i) {return pnts[i] + p.pnts[i];}
    Point3d<T> sub(Points &p, int i) {return pnts[i] - p.pnts[i];}
    Point3d<T> mlt(Points &p, int i) {return pnts[i] * p.pnts[i];}
};

template <class T> int
Points<T>::assign(Points &p1, Points &p2) {
    int s = min(p1.size(), p2.size());
    assign(s);

    return s;
}

template <class T> bool
Points<T>::isSameSize(Points &p, char *s) {
    bool    same = (pnts.size() == p.pnts.size());

    #ifndef POINTS_NO_THROW
    if (!same) {
        PointsException e;
        e.setMessage((string) s + " source sizes not equal");
        throw e;
    }
    #endif

    return same;
}

template <class T> Points<T>
Points<T>::doOpp(Points &p, oppFunc f) {
    isSameSize(p, __FUNCTION__);
    Points<T>   r;
    int         s = r.assign(*this, p);
    Point3d<T>  q;

    for (int i = 0; i < s; i++) {
        q = f(p, i); // Causes error C2064 but q = add(p, i); works fine
    }

    return r;
}


int
main () {
    Points<>    p, q;

    q.assign(a, 4);
    p = q + q;

    return 1;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

_失温 2024-12-04 18:38:45

由于 oppFunc f 是成员函数指针,因此您应该使用特殊语法通过成员函数指针进行调用:

q = (this->*f)(p, i);

Since oppFunc f is a member function pointer you should use the special syntax to make a call through a member function pointer:

q = (this->*f)(p, i);
生死何惧 2024-12-04 18:38:45

这应该是:

Points operator-(Points &){return doOpp(p, &Points<T>::sub);};

不是:

 Points operator-(Points &p){return doOpp(p, &Points<T>::sub);}; // note the p argument

Should this:

Points operator-(Points &){return doOpp(p, &Points<T>::sub);};

not be:

 Points operator-(Points &p){return doOpp(p, &Points<T>::sub);}; // note the p argument
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文