Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Downhill Solver -- error due to pure virtual functions

I'm familiarizing with Optimization Algorithm in OpenCV. I've found an interesting example to start with:

http://answers.opencv.org/question/33676/opencv-dev-300-downhillsimplex-method-opencv-error-assertion-failed/

so, I developed some code based on that:

#include <cstdlib>
#include <cmath>
#include <algorithm>

using namespace cv;
using namespace std;


class DistanceToLines :public MinProblemSolver::Function
{
public:
    double calc(const double* x)const
    {

        return x[0] * x[0] + x[1] * x[1];
    }
};


int main()
{

    Ptr<DownhillSolver> DHS = DownhillSolver::create();


//    Ptr<MinProblemSolver::Function> ptr_F = makePtr<DistanceToLines>();  // NOT WORKING!!!
    Ptr<MinProblemSolver::Function> ptr_F(new DistanceToLines());                // NOT WORKING!!!

    DHS->setInitStep(0.01);
    DHS->setFunction(ptr_F);

    return 0;

}

It gives me the following error, when building:

D:\ALGORITMI\minDownhill\main.cpp:50: error: invalid new-expression of abstract class type 'DistanceToLines'
     Ptr<MinProblemSolver::Function> ptr_F(new DistanceToLines());
D:\ALGORITMI\minDownhill\main.cpp:33: note:   because the following virtual functions are pure within 'DistanceToLines':
 class DistanceToLines :public MinProblemSolver::Function

any hint? I saw that there are not many examples on the web about these methods, so I cannot figure out what is happening.

Downhill Solver -- error due to pure virtual functions

I'm familiarizing with Optimization Algorithm in OpenCV. I've found an interesting example to start with:

http://answers.opencv.org/question/33676/opencv-dev-300-downhillsimplex-method-opencv-error-assertion-failed/

so, I developed some code based on that:

#include <cstdlib>
#include <cmath>
#include <algorithm>

using namespace cv;
using namespace std;


class DistanceToLines :public MinProblemSolver::Function
{
public:
    double calc(const double* x)const
    {

        return x[0] * x[0] + x[1] * x[1];
    }
};


int main()
{

    Ptr<DownhillSolver> DHS = DownhillSolver::create();


//    Ptr<MinProblemSolver::Function> ptr_F = makePtr<DistanceToLines>();  // NOT WORKING!!!
    Ptr<MinProblemSolver::Function> ptr_F(new DistanceToLines());                // NOT WORKING!!!

    DHS->setInitStep(0.01);
    DHS->setFunction(ptr_F);

    return 0;

}

It gives me the following error, when building:

D:\ALGORITMI\minDownhill\main.cpp:50: error: invalid new-expression of abstract class type 'DistanceToLines'
     Ptr<MinProblemSolver::Function> ptr_F(new DistanceToLines());
D:\ALGORITMI\minDownhill\main.cpp:33: note:   because the following virtual functions are pure within 'DistanceToLines':
 class DistanceToLines :public MinProblemSolver::Function

any hint? I saw that there are not many examples on the web about these methods, so I cannot figure out what is happening.