Reputation: 38
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
Upvotes: 1
Views: 210
Reputation:
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.
Upvotes: 0