类将对其自身的引用传递给具有accumulate 的函子

发布于 2024-12-12 19:29:43 字数 2109 浏览 3 评论 0原文

我有一个函数是从另一个类派生的,而另一个类又是从虚拟基类派生的。

该函数内部是一个函子。仿函数需要能够访问该函数及其父类内部的所有对象。但是,当传递对“this”或函数名称的引用时,我收到错误。

所有这些复杂性的原因是为了加快代码速度。我正在编写的算法实际上只有一个昂贵的部分。我的目的是并行化该部分。然而,该步骤通过几个步骤同时累加两个值。所以,我需要重写一些运算符。函子似乎是实现这一点的最简单方法。

函子需要能够访问 myClassA 和 myClassB 中的对象,因此我需要使用对 myClassB 的引用来构造它。 (由于 B 从 A 下降,那么所有的都应该是可访问的。)

问题是,当我尝试将函子传递到累加行时,我收到有关没有匹配函数的错误。我尝试过“this”、“*this”、“myClassA”、“myClassB”等。

有什么想法吗?

class myBaseClass {
public:
    virtual double doFancyStuff(double a, double b)=0;
    virtual double doOtherFancyStuff(double a, double b)=0;
}

class myClassA : public myBaseClass { // B inherits a bunch of stuff from myClassA
    public:
        double doFancyStuff(double a, double b){
            double C = pow(a,b); //dummy action, is actually a lot of fancy math here
            return C;
        }
        virtual double doOtherFancyStuff(double a, double b)=0;     
}

class myClassB : public myClassA { // B inherits a bunch of stuff from myClassA
    public:
         double doOtherFancyStuff(double a, double b){
            return pow(a * b, 3);  //dummy action, is actually a lot of fancy math here
        }

    double doMoreFancyStuff(double a, double b){
        // do some stuff

        struct MyFunctor : public binary_function<Fraction, DoubleIterator, Fraction> {
            MyFunctor(myClassB& in_class, doubleA) : myClassA(in_class), column(iColumn) {} // Note: constructed with reference to myClassB, so it can access all the objects (functions, data) in myClassB                
            myClassB& myClassB;
            doubleA;
            double B = 123.456;

            double operator()(double B, double A, ) {
                double C = doFancyStuff(A,B);
                C += doOtherFancyStuff(A,B,C);
                return C;
            }
        }
        A = 1234 // actually result of some other work in the real code.
        //use stl to accumulate
        accumulate(data.begin(), data.end(), temp, MyFunctor(this,  A) );  //Passing 'this' here so that functor gets a reference to myClassB
    }

I have a function that is descended from another class which in turn descends from a virtual base class.

Inside that function is a functor. The functor needs to be able to access all the objects inside of this function and its parent class. But, when passing a reference to "this" or to the function name, I get an error.

The reason for all this complication is to speed up the code. The algorithm I'm coding really only has one expensive part. My intent is to parallelize that part. However, that step accumulates over two values simultaneously, through several steps. So, I need to override some operators. A functor seemed like the easiest way to implement this.

The functor needs to be able to access objects in myClassA and myClassB, so I need to construct it with a reference to myClassB. (Since B descends from A, then all should be accesible.)

The problem is that when I try to pass the functor into the accumulate line, I get errors about no matching function. I've tried "this", "*this", "myClassA", "myClassB", etc.

Any ideas??

class myBaseClass {
public:
    virtual double doFancyStuff(double a, double b)=0;
    virtual double doOtherFancyStuff(double a, double b)=0;
}

class myClassA : public myBaseClass { // B inherits a bunch of stuff from myClassA
    public:
        double doFancyStuff(double a, double b){
            double C = pow(a,b); //dummy action, is actually a lot of fancy math here
            return C;
        }
        virtual double doOtherFancyStuff(double a, double b)=0;     
}

class myClassB : public myClassA { // B inherits a bunch of stuff from myClassA
    public:
         double doOtherFancyStuff(double a, double b){
            return pow(a * b, 3);  //dummy action, is actually a lot of fancy math here
        }

    double doMoreFancyStuff(double a, double b){
        // do some stuff

        struct MyFunctor : public binary_function<Fraction, DoubleIterator, Fraction> {
            MyFunctor(myClassB& in_class, doubleA) : myClassA(in_class), column(iColumn) {} // Note: constructed with reference to myClassB, so it can access all the objects (functions, data) in myClassB                
            myClassB& myClassB;
            doubleA;
            double B = 123.456;

            double operator()(double B, double A, ) {
                double C = doFancyStuff(A,B);
                C += doOtherFancyStuff(A,B,C);
                return C;
            }
        }
        A = 1234 // actually result of some other work in the real code.
        //use stl to accumulate
        accumulate(data.begin(), data.end(), temp, MyFunctor(this,  A) );  //Passing 'this' here so that functor gets a reference to myClassB
    }

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

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

发布评论

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

评论(1

旧伤慢歌 2024-12-19 19:29:43

您应该将函子移到函数之外。

未实现全新 C++11 标准的编译器不允许像 accumulate 这样的模板用本地类实例化。

You should move the functor outside the function.

Compilers not implementing the brand new C++11 standard does not allow templates like accumulate to be instantiated with a local class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文