std::inner_product in class template

Sep 23, 2019 at 8:15pm
Hi!

I encounter a problem when I tried to use inner product inside the class (I need to subtract the absolute values of two vectors). I cannot pass my own binaryOperaton function as an argument to inner_product(...) when this binaryOperation is defined inside the class.

My class look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
template <int dim>
class Convergence
{
    public:
// ...other stuff ...
    double myaccumulator(double x, double y)
    {
	return x + std::abs(y);
    }

    double myproduct(    double x, double y)
    {
	return x - y;
    }

    double calculate_differences(std::vector<double> vect1,
                                 std::vector<double> vect2)
    {
      return std::inner_product(std::begin(vect1),
                                std::end(vect1),
                                std::begin(vect2),
                                0,
                                myaccumulator,
                                myproduct);
    }
// ... other stuff ...
}


The following error appears:
error: invalid use of non-static member function ‘double Convergence<dim>::myaccumulator(double, double) [with int dim = 2]’

Can someone tell me what is incorrect in this implementation?
Last edited on Sep 23, 2019 at 8:20pm
Sep 23, 2019 at 8:39pm
Declare the functions myaccumulator and myproduct as static since they don't rely on class data.
Sep 24, 2019 at 10:32am
Yes, that works! Thank you very much!
Topic archived. No new replies allowed.