1

I am supposed to write a program which has to generate a triangle function, calculate the derivative using forward and backward divided differences and differentiate the triangle function. So, I wrote some code and have only one problem:

include\MyClass.h|12|note: no known conversion for argument 1 from 'float (MyClass::)(int, float, float)' to 'float ()(int, float, float)'|

My code:

main.cpp

#include <iostream>
#include "MyClass.h"
using namespace std;

int main()
{
    MyClass object (3,4,2,0.1);

    for (float i=object.x; i<2.5; i+=0.01)
    {
        cout << object.Triangle(10, 3.14, i) << " ";
    }

    cout << "////////////////////";

    for (float i=object.x; i<2.5; i+=0.01)
    {
        cout << object.Derivative(&object.Triangle, i, object.con) << " ";
    }
}

MyClass.h

#ifndef MYCLASS_H
#define MYCLASS_H


class MyClass
{
    public:
        MyClass();
        MyClass(int k_max, float omega, float x, float con);
        ~MyClass();
        float Triangle (int k_max, float omega, float x);
        float Derivative (float (*w) (int k_max, float omega, float x), float var, float con);
        float DerivativeCntr (float (*w) (int k_max, float omega, float x), float var, float con);
        int k_max;
        float omega, x, result, con;
};

#endif // MYCLASS_H

MyClass.cpp

#include "MyClass.h"

MyClass::MyClass() {}
MyClass::~MyClass() {}

MyClass(int K_max, float Omega, float X, float Con)
{
    k_max=K_max;
    omega=Omega;
    x=X;
    con=Con;
}

///////////////////////////////////////////////

float Triangle (int k_max, float omega, float x)
    {
        result=0;

        for int (i=0; i<=k_max; i++)
        {
            result += ( 8*pow(-1, i)*(sin((2*i+1)*omega*x ) / ( pow(2*i+1, 2) * pow(M_PI, 2) )
        }
        return result;
    }

///////////////////////////////////////////////

float Derivative (float (*w) (int k_max, float omega, float x), float var, float con)
    {
        float result = (w(10, 3.14, var+con) - w(10, 3.14, var))/var;
        return result;
    }

///////////////////////////////////////////////

float DerivativeCntr (float (*w) (int k_max, float omega, float x), float var, float con)
    {
        float result=(w(10, 3.14, var)-w(10, 3.14, var-con))/2*var;
        return result;
    }

I would really appreciate your help, thanks!

EDIT: I've got this program working, but it's recommended to use a class and required to use a pointer to the function. That's my non object-oriented code: https://ideone.com/mtPLAo

  • Member function pointers are not directly convertible to non-member function pointers. – François Andrieux Dec 04 '17 at 19:33
  • You have a mismatch of parentheses in `MyClass.h` –  Dec 04 '17 at 19:34
  • Possible duplicate? https://stackoverflow.com/questions/2402579/function-pointer-to-member-function – François Andrieux Dec 04 '17 at 19:35
  • @Tobias I am total noob in C++. What should i do? – Tomasz Jankowski Dec 04 '17 at 19:36
  • Count the parenthesis and make sure the opens and the closes balance. If they don't add the ones that are missing. – user4581301 Dec 04 '17 at 19:40
  • This stuff is non-trivial and requires a lot of answering to get right. Here's a really good document on method pointers and how to use them: https://isocpp.org/wiki/faq/pointers-to-members – user4581301 Dec 04 '17 at 19:51
  • Come to think of it, as a total noob, you shouldn't have to be doing this. The assignment almost certainly wants you to do something else (or the instructor is a nasty ). Give us a bit more context on the problem and maybe we can help figure out a better way to do it. – user4581301 Dec 04 '17 at 19:55

1 Answers1

0

You have several errors of syntactical nature in your code.

In MyClass.h, change into

float Derivative (float *w, int k_max, float omega, float x, float var, float con);
float DerivativeCntr (float *w, int k_max, float omega, float x, float var, float con);

In MyClass.cpp, all member functions should be prefixed by MyClass:: and also the same for the constructor that takes arguments.

  • Now I've got error "candidate expects 6 arguments, 3 provided" in main. Those functions are wrote like this, because they should contain a pointer to "Triangle" function. – Tomasz Jankowski Dec 04 '17 at 19:46
  • 1
    One more mistake being made that should be brought to Askers attention: You can't do this: `&object.Triangle`. Need `&MyClass::Triangle` and to supply the instance later. That or look at `std::bind` and lambdas. – user4581301 Dec 04 '17 at 19:47
  • Two more. Forgot about the one @FrançoisAndrieux mentioned above. – user4581301 Dec 04 '17 at 19:49