0

I'm trying to implement this code for Linear Regression here but it is unable to compile due to std::sort returning several errors.

#include "LinearRegression.h"
#include <iostream>
#include <algorithm>
#include <vector>

bool LinearRegression::custom_sort(double a, double b) /*sorts based on absolute min value or error*/
{
    double a1 = abs(a-0);
    double b1 = abs(b-0);
    return a1<b1;
}

void LinearRegression::predict()
{
    /*Intialization Phase*/
    double x[] = { 1, 2, 4, 3, 5 }; //defining x values
    double y[] = { 1, 3, 3, 2, 5 }; //defining y values
    double err;
    double b0 = 0; //initializing b0
    double b1 = 0; //initializing b1
    double alpha = 0.01; //intializing error rate
    std::vector<double>error; // array to store all error values

    /*Training Phase*/
    for (int i = 0; i < 20; i++) //
    {   
        int idx = i % 5; //for accessing index after every epoch
        double p = b0 + b1 * x[idx]; //calculate prediction
        err = p - y[idx]; //calculate error
        b0 = b0 - alpha * err; //update b0
        b1 = b1 - alpha * err * x[idx]; // updating b1
        std::cout << "B0=" << b0 << " " << "B1=" << b1 << " " << "error=" << err << std::endl; //print values after every update
        error.push_back(err);
    }
    std::sort(error.begin(),error.end(),custom_sort); //sorting based on error values
    std::cout << "Final Values are: " << "B0=" << b0 << " " << "B1=" << b1 << " " << "error=" << error[0] << std::endl;

    /*Testing Phase*/
    std::cout << "Enter a test x value";
    double test;
    std::cin >> test;
    double pred = b0 + b1 * test;
    std::cout << std::endl;
    std::cout << "The value predicted by the model= " << pred;
}

I get the errors:

non-standard syntax; use '&' to create a pointer to member

and

no matching overloaded function found

for this line

std::sort(error.begin(),error.end(),custom_sort); 
ahmadalibin
  • 133
  • 8
  • The error message actually contains the solution for your problem: "use '&' to create a pointer to member". To get a pointer to a class member you must use the address-of operator `&`. Also note that unless `custom_sort` is a `static` member function, you need an object to call the function on. – Some programmer dude Dec 08 '20 at 07:37
  • See [problem sorting using member function as comparator](https://stackoverflow.com/q/1902311/580083). BTW, you might want to replace `abs` with `std::abs` or `std::fabs`, see [here](https://stackoverflow.com/questions/33738509/whats-the-difference-between-abs-and-fabs). – Daniel Langr Dec 08 '20 at 07:37

1 Answers1

5

custom_sort is a non-static member function, it can't work with sort directly because under the hood it expects three arguments, not two - the first one is the object pointer.

In your particular example, custom_sort doesn't use any data members, so it can be made static (static should go into the declaration):

static bool LinearRegression::custom_sort(double a, double b);

Then you could write:

std::sort(error.begin(), error.end(), &custom_sort);

Note that you need & to form a pointer to a member function, as the error message suggests. However, for static member function it can be omitted (similar to free functions).

Evg
  • 25,259
  • 5
  • 41
  • 83
  • 2
    Once the `custom_sort` function is declared as `static`, you don't need the `&LinearRegression::` - as it's being called from within that class. – Adrian Mole Dec 08 '20 at 07:42