About pointers to a double derived class

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.
Last edited on
How many functions do you see here:
virtual double Eval(double radius);
virtual double Eval(double radius) const;
Hi keskiverto, thanks for the reply.
That was the problem.
What if I write another "virtual double Eval(double radius)" function inside public FunzioneBase class without "const" restriction?

I need "virtual double Eval(double radius) const" with that "const" for other purposes I did not mention in the code I posted(other "funcition" classes which required "const" ). Will it work anyway?
Thanks again!
Last edited on
once you make `sfera' a concrete class, to create a `sfera' do sfera foo(3);

> I need to modify components of "Vettore" inside this function
¿why? you are just setting random numbers, ¿do you care about that state at all?


ps, fix your indentation.
Hello ne555, thanks for giving me your opinion.
Actually I'm applying this method for evaluating the volume of a sphere: https://en.wikipedia.org/wiki/Monte_Carlo_integration which requires random numbers.
The focus of the program is to verify the evolution of the S(n)/R(n) ratio, where S(n) is the volume of the n-dimensional sphere of radius 1 and R(n) is the volume of the n-dimensional "cube" (which Side is equal to 2) circumscribed to the sphere, using Monte Carlo Integration method. I'm trying to write it 3-dimensional.
I'm still working to get it run correctly, anyway I need those random numbers.


So the difference between declaring:
FunzioneBase* foo = new sfera(3);
and
Vettore* foo = new sfera(3);

what concerns?
Last edited on
> I'm still working to get it run correctly, anyway I need those random numbers.
my point is that you don't need them as the state of your object.
you may create a local `Vettore', or simply sum the numbers directly.
1
2
3
4
double d = 0;
for(int i=0; i<this->_dimensions; ++i)
   d += 2*pow(radius*k.LCG(), 2);
d = sqrt(d);



> So the difference between declaring:
¿why are you using new?
http://www.cplusplus.com/forum/general/138037/#msg731921
Topic archived. No new replies allowed.