没有已知的转换&#float;(MyClass :: *)'到'浮(*)'

时间:2017-12-04 19:30:57

标签: c++

我应该编写一个必须生成三角函数的程序,使用前向和后向分割差异计算导数并区分三角函数。 所以,我写了一些代码并且只有一个问题:

  

include \ MyClass.h | 12 |注意:没有已知的转换参数1来自' float(MyClass :: )(int,float,float)' to' float()(int,float,float)' |

我的代码:

的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;
    }

非常感谢你的帮助,谢谢!

编辑: 我已经使用了这个程序,但是建议使用一个类并且需要使用指向该函数的指针。这是我的非面向对象代码: https://ideone.com/mtPLAo

1 个答案:

答案 0 :(得分:0)

您的代码中存在一些语法错误。

MyClass.h中,转到

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);

MyClass.cpp中,所有成员函数都应以MyClass::为前缀,对于带参数的构造函数也应该相同。