|
-
November 1st, 2010, 04:45 AM
#1
Member function as function argument
I'm having problems with using a simple static function I have defined & declared to calculate the definite integral of a function that is passed to it as an argument. Sorry in advanced for posting so much code. Your help is very much appreciated.
Math.h _____________________________________________________
class Math
{
public:
static double integrate(double (*f)(double x), double a, double b);
};
Math.cpp _____________________________________________________
#include "Math.h"
double Math::integrate(double (*f)(double x), double a, double b)
{
int m = 1000;
double dt = (b-a)/m;
double c = 0;
for(int i = 0; i<m; i++)
{
c += (*f)(a+i*dt);
}
return c/m;
}
Model.h _______________________________________________________
#include <cmath>
#include "Math.h"
#define pi 3.141592
class Model
{
private:
double y, z;
double C;
double L;
double R_L;
double V_in;
double V_out;
double V_f;
double RDSON;
Math math;
double tau_charging();
double T_charging();
double omega();
double T_discharging();
double F_switching();
double L_max();
double i_L_charging(double t);
double I_d_avg();
double R_D();
double i_L_discharging(double t);
double E_L_charging(double t);
double E_L_discharging(double t);
double E_R_L_charging(double t);
double E_R_L_discharging(double t);
double E_RDSon_charging(double t);
double E_D_discharging(double t);
double Eff_charging(double t);
double Eff_discharging(double t);
double Eff_avg_charging();
double Eff_avg_discharging();
public:
double Eff_avg();
double P_out();
double MaxLoad();
void set_y(double a);
void set_z(double a);
void set_C(double a);
void set_L(double a);
void set_R_L(double a);
void set_V_in(double a);
void set_V_out(double a);
double get_y();
double get_z();
double get_C();
double get_L();
double get_R_L();
double get_V_in();
double get_V_out();
};
Model.cpp _____________________________________________
#include "Model.h"
double Model::tau_charging()
{
return L/(R_L+RDSON);
}
double Model::T_charging()
{
return y*tau_charging();
}
double Model: mega()
{
return 1.0/sqrt(L*C);
}
double Model::T_discharging()
{
return (2*pi)/(4*omega());
}
double Model::F_switching()
{
return 1/(T_charging() + T_discharging());
}
double Model::L_max()
{
return 0;
}
double Model::i_L_charging(double t)
{
return V_in/(R_L+RDSON) - V_in/(R_L+RDSON)*exp(-t/tau_charging());
}
double Model::I_d_avg()
{
return math.integrate(&Model::i_L_charging, 0.0, T_charging());
}
double Model::R_D()
{
return V_f/I_d_avg();
}
double Model::i_L_discharging(double t)
{
return exp((t*(R_D()+R_L))/(2*z*L_max()))*cos(omega()*t)*i_L_charging(T_charging());
}
double Model::E_L_charging(double t)
{
return 0.5*z*L_max()*pow(i_L_charging(t),2);
}
double Model::E_L_discharging(double t)
{
return 0.5*z*L_max()*pow(i_L_discharging(t),2);
}
double Model::E_R_L_charging(double t)
{
return 0;
}
double Model::E_R_L_discharging(double t)
{
return 0;
}
double Model::E_RDSon_charging(double t)
{
return 0;
}
double Model::E_D_discharging(double t)
{
return 0;
}
double Model::Eff_charging(double t)
{
return E_L_charging(t)/(E_L_charging(t)+E_R_L_charging(t)+E_RDSon_charging(t));
}
double Model::Eff_discharging(double t)
{
return E_L_discharging(t)/(E_L_discharging(t)+E_R_L_discharging(t)+E_D_discharging(t));
}
double Model::Eff_avg_charging()
{
return 0;
}
double Model::Eff_avg_discharging()
{
return 0;
}
double Model::Eff_avg()
{
return (Eff_avg_charging()+Eff_avg_discharging())/2;
}
double Model::P_out()
{
return (E_L_discharging(0.0)-E_R_L_discharging(T_discharging())-E_D_discharging(T_discharging()))/(T_charging()+T_discharging());
}
double Model::MaxLoad()
{
return pow(V_in,2)/P_out();
}
/*
Get and set functions
*/
void Model::set_y(double a)
{
y=a;
}
void Model::set_z(double a)
{
z=a;
}
void Model::set_C(double a)
{
C=a;
}
void Model::set_L(double a)
{
L=a;
}
void Model::set_R_L(double a)
{
R_L=a;
}
void Model::set_V_in(double a)
{
V_in=a;
}
void Model::set_V_out(double a)
{
V_out=a;
}
double Model::get_y()
{
return y;
}
double Model::get_z()
{
return z;
}
double Model::get_C()
{
return C;
}
double Model::get_L()
{
return L;
}
double Model::get_R_L()
{
return R_L;
}
double Model::get_V_in()
{
return V_in;
}
double Model::get_V_out()
{
return V_out;
}
The error from GCC compiler is:
D:\My Projects\In progress\Solar panel system\My inverter\Calculations\Mathematica\C++\DC-DC Converter\Model.cpp||In member function `double Model::I_d_avg()':|
D:\My Projects\In progress\Solar panel system\My inverter\Calculations\Mathematica\C++\DC-DC Converter\Model.cpp|40|error: no matching function for call to `Math::integrate(double (Model::*)(double), double, double)'|
D:\My Projects\In progress\Solar panel system\My inverter\Calculations\Mathematica\C++\DC-DC Converter\Math.h|5|note: candidates are: static double Math::integrate(double (*)(double), double, double)|
||=== Build finished: 1 errors, 0 warnings ===|
Please note that this DOES work:
main.cpp ________________________________________________________
#include <iostream> // For UI I/O
#include <cmath>
#include <fstream>
#include "Model.h"
using namespace std;
double f(double a);
int main()
{
Model m;
cout << "Integrating..." << endl;
cout << "The answer is: " << Math::integrate(f,0.0,1.0);
cout << endl;
system("pause");
return 0;
}
double f(double a)
{
return a;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|