Dear folk,
Please help me and let me ask a stupid question.
I declared variable x as DeriveType (DeriveType is a class).
I want to assign an int y to x, e.g
int y = x
But when I compile, the compiler shows the error
error: cannot convert 'const DerivType' to 'int'
Please tell me how to solve this problem.
Thank you very much.
Yours sincerely,
This is the Class DerivType
class DerivType;
typedef DerivType (*ddf_FctPtr)(const DerivType&);
class DerivType { private: interval f, df, ddf;
public: DerivType ( );
DerivType ( const interval&, const interval&, const interval& );
DerivType ( const DerivType& );
DerivType& operator= ( const DerivType& );
friend DerivType DerivConst ( const real& );
friend DerivType DerivConst ( const interval& );
friend DerivType DerivVar ( const real& );
friend DerivType DerivVar ( const interval& );
friend inline const interval fValue ( const DerivType& );
friend inline const interval dfValue ( const DerivType& );
friend inline const interval ddfValue ( const DerivType& );
friend DerivType operator+ ( const DerivType& );
friend DerivType operator- ( const DerivType& );
....
And I have a function likes this.
DerivType f ( const DerivType& x )
{
DerivType result;
DerivType xx;
double sum;
xx = x;
//Convert x from DerivType to double
void *pVoidx = &xx; [1]
double *pDoublex = static_cast<double*>(pVoidx); [2]
MLPutReal(lp, *pDoublex);
MLGetReal(lp, &sum);
//Convert from double to DerivType for the return value of function
printf( "x = %f, result = %f\n", *pDoublex, sum);
void *pSum=∑ [3]
DerivType *pDerivTypeSum = static_cast<DerivType*>(pSum); [4]
return *pDerivTypeSum;
}
This function work properly. But my teacher said that it is not the good code, because even the code works in this case, it is still implementation-dependent
and architecture-dependent. (He gives me the link:
http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=195[
^])
Can you help me to improve this code, namely [1], [2], [3], [4].
My idea is how to convert from DerivType to double/int and conversely.
Thank for your reading and help. I am very appriciate about your hints.
Your sincerely
Thank you very much
Yours sincerely,