Hello forum,
I have been struggling with this problem since a while.
Basically I have a FunzioneBase.h that works like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
#include "Vettore.h" //Vettore.h is acually an header file of a class which create some kind of container of numbers of selected dimension.
//I need this class instead of a <vector> for other purposes of the program, but the problem is not concerning this section.
class FunzioneBase {
public:
virtual double Eval(double x) const =0; //line n°9, remeber this line, we'll get to that later
};
class sfera: public FunzioneBase, public Vettore { //we see that sfera class is derived from two different base classes.
//This is line n° 47, remeber this line because it wil be inside the compilation error
public:
sfera(int dimensions);
~sfera();
virtual double Eval(double radius);
protected:
double _dimensions;
};
|
and its implementation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
sfera::sfera(int dimensions): Vettore(dimensions) { _dimensions = dimensions;} //constructor which assigns a position vector in space of requested dimension
sfera::~sfera() {}
double sfera::Eval(double radius){
//doing stuff.. I left out "const" because I need to modify components of "Vettore" inside this function, otherwise it will give a compilation error.
//I'll post the code anyway, maybe it will give you more infos to help me solve the problem:
Random k(3);
double d = 0;
for(int i = 0; i < m_N; i++) //m_N is the dimension of declared Vettore
SetComponent(i, sqrt(2*pow(radius*k.LCG(), 2) ); //SetComponent, GetComponent are Vettore.h methods, which intuitively you can imagine their purposes.
//Random k(3) and LCG() basically let me generate a random number between 0 and 1.
double expected = pow(radius, _dimensions)*(pow(M_PI, _dimensions/2))/(tgamma(_dimensions/2 + 1));
for(int i = 0; i < m_N; i++)
d += pow(GetComponent(i), 2);
d = sqrt(d);
double observed = pow(d, _dimensions)*(pow(M_PI, _dimensions/2))/(tgamma(_dimensions/2 + 1));
if(observed <= expected)
return 1;
else
return 0;
}
|
The problem occurs when I need to define a ponter to sfera, which happens in another .h file, "Integral.h", whose constructor works like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
class Integral {
public:
Integral(double a, double b, FunzioneBase* function);
//metohds..
private:
FunzioneBase* _integrand;
};
//Integral.h constructor is:
Integral::Integral(double a, double b, FunzioneBase* function) {
_integrand = function;
/* _a = min(a,b);
_b = max(a,b);
if (a > b) _sign = -1;
else _sign = 1; */ //we don't need those right now
}
|
I acutally need a pointer to FunzioneBase because I need that virtual Eval function declared in FunzioneBase.h in order to evaluate the integral of a "sphere", but it seems that isn't enough to let the Integral.h method workf, which is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
double Integral::HitOrMiss2(int nstep) {
double area = 8;
int n = 0;
int passaggi = 0;
do {
if( _integrand->Eval(1) == 1)
n++;
passaggi++;
} while( passaggi < nstep);
return area*n/nstep;
}
|
I ended up getting this error:
main.c:13 error: cannot allocate an object of abstract type 'sfera'
FunzioneBase.h:47: note: because the following virtual functions are pure within 'sfera':
FunzioneBase.h:9: note: virtual double FunzioneBase::Eval(double) const
I delcared this ponter in main.c as:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
int main(){
FunzioneBase* function = new sfera(3); //line n° 13
Integral* p = new Integral(0, 2, function);
cout<<p->HitOrMiss2(10000)<<endl;
return 0;
}
|
I think the problem is related to that pointing to "sfera" class. I wondered what would happen if I had wrote Vettore* foo = new sfera(3). Which is the correct syntax in order to avoid that error? Hope you guys can help me, it would be really appreciated.