error C2296: '.*' : illegal, left operand has type 'cStepCurveEvaluator *const '

B

BlueTrin

Hello I was adapting a C version of SolvOpt in C++ to use it within a
virtual class.

However I am stuck with the overriding of evaluation and gradiant
functions.



cStepCurveEvaluator.cpp
cStepCurveEvaluator.cpp(14) : error C2296: '.*' : illegal, left operand
has type 'cStepCurveEvaluator *const '
cStepCurveEvaluator.cpp(15) : error C2296: '.*' : illegal, left operand
has type 'cStepCurveEvaluator *const '
cStepCurveEvaluator.cpp(16) : error C2296: '.*' : illegal, left operand
has type 'cStepCurveEvaluator *const '


The Class I defined is:

class cSolvOpt
{
private:
virtual double fun(double x[]) = 0;
virtual double func(double x[]) = 0;
virtual double grad(double x[],double g[]) = 0;
virtual double gradc(double x[],double g[]) = 0;

public:
cSolvOpt(void);
~cSolvOpt(void);

double cSolvOpt::solvopt(unsigned short n,
double x[],
double options[]
);

void cSolvOpt::apprgrdn ( unsigned short n,
double g[],
double x[],
double f,
double (cSolvOpt::*fun)(double*),
double deltax[],
unsigned short obj);
};


And I would like to override only fun as func, grad and gradc are not
used. The class cSolvOpt is checking if func grad and gradc a function
pointers equal to NULL.

Is there any way to NOT override these members or to initialize them to
NULL ?

Cheers
Anthony
 
H

Howard

BlueTrin said:
Hello I was adapting a C version of SolvOpt in C++ to use it within a
virtual class.

However I am stuck with the overriding of evaluation and gradiant
functions.



cStepCurveEvaluator.cpp
cStepCurveEvaluator.cpp(14) : error C2296: '.*' : illegal, left operand
has type 'cStepCurveEvaluator *const '
cStepCurveEvaluator.cpp(15) : error C2296: '.*' : illegal, left operand
has type 'cStepCurveEvaluator *const '
cStepCurveEvaluator.cpp(16) : error C2296: '.*' : illegal, left operand
has type 'cStepCurveEvaluator *const '

You don't show where the code is which is generating these errors. What's a
cStepEvaluator?
The Class I defined is:

class cSolvOpt
{
private:
virtual double fun(double x[]) = 0;
virtual double func(double x[]) = 0;
virtual double grad(double x[],double g[]) = 0;
virtual double gradc(double x[],double g[]) = 0;

public:
cSolvOpt(void);
~cSolvOpt(void);

double cSolvOpt::solvopt(unsigned short n,
double x[],
double options[]
);

void cSolvOpt::apprgrdn ( unsigned short n,
double g[],
double x[],
double f,
double (cSolvOpt::*fun)(double*),
double deltax[],
unsigned short obj);
};


And I would like to override only fun as func, grad and gradc are not
used. The class cSolvOpt is checking if func grad and gradc a function
pointers equal to NULL.

Is there any way to NOT override these members or to initialize them to
NULL ?

You cannot instantiate a class which has any "pure virtual" member
functions. That's what you have above, with the "= 0" specified after each
function. You need to override those and provide bodies for them if you
want to actually create an instance of a class derived from cSolvOpt. If
you don't use the functions, just take some appropriate action, such as
returning 0, or throwing an exception. But they do need to be implemented.

-Howard
 
B

BlueTrin

Sorry, here is the class. I would like to assign 0 to func, grad and
gradc (a NULL pointer), is that invalid in C++ ?

cStepCurveEvaluator::cStepCurveEvaluator(vector<cMultiVarPolynom*>&
vec_Equs, double *dblEquVals)
: vec_Equations(vec_Equs)
, dbl_Equations_values(dblEquVals)
{
this->*func = NULL;
this->*grad = NULL;
this->*gradc = NULL;
}

class cStepCurveEvaluator
: public cSolvOpt
{
private:
double* CalcValues(double * values);
double fun(double * values);
double func(double x[]) {return 0.0;};
double grad(double x[],double g[]) {return 0.0;};
double gradc(double x[],double g[]) {return 0.0;};
public:
vector<cMultiVarPolynom*>& vec_Equations;
double* dbl_Equations_values;

cStepCurveEvaluator(vector<cMultiVarPolynom*>& vec_Equs, double
*dblEquVals);
~cStepCurveEvaluator(void) {;}
};

Compiling...
cStepCurveEvaluator.cpp
cStepCurveEvaluator.cpp(13) : fatal error C1001: INTERNAL COMPILER
ERROR
(compiler file 'msc1.cpp', line 2701)
Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more
information

Build log was saved at "file://c:\Users\Stusd\GenLib
0.0.5\GenLib-IRD\Debug\BuildLog.htm"
GenLib-IRD - 1 error(s), 0 warning(s)
 
B

BlueTrin

Ok I got it, I need to implement them ...

Is there any way to assign a null pointer to a member function ? is
that forbidden in C++ ?
 
H

Howard

BlueTrin said:
Ok I got it, I need to implement them ...

Is there any way to assign a null pointer to a member function ? is
that forbidden in C++ ?

It's not possible in C++.

You can assign NULL to a pointer-to-a-function (member or otherwise), but
that's not the same thing.

The name of the function is not a pointer to the function (although it's
conceivable that someone could implement their compiler that way, I
suppose). When you add "= 0" to the declaration of the function in the
class definition, you tell the compiler it's a "pure virtual" function.
There is no way to tell the compiler, other than making a function pure
virtual, that you don't want to implement it. And if it's not implemented,
then you can't (directly) create an instance of the class.

Just implement the functions, giving them some kind of simplistic behavior
(including throwing an exception?) and document somewhere that they should
not be used.

-Howard
 
B

Bernd Strieder

Hello,
Ok I got it, I need to implement them ...

Is there any way to assign a null pointer to a member function ? is
that forbidden in C++ ?

No, you cannot assign anything to a member function. The = 0 in the
class definition makes the class an abstract class, i.e. there are no
direct instances of that class, but only of subclasses implementing
those methods and not making the subclass abstract again. It is
possible to provide an implementation of a method and declaring it
abstract, i.e. the =0 is completely orthogonal to providing an
implementation. Both cannot be changed at runtime.

You could use member function pointers to get something you can "assign"
different methods of the same class with the same signature to at
runtime.

class abc
{
public:
abc();

char func1(int){}

typedef char (abc::*MemberFctPtr)(int);
MemberFctPtr mfp;
};

abc::abc()
: mfp(&abc::func1)
{
// mfp = &abc::func1; // assignment
}

Bernd
 
B

BlueTrin

Thank you for your answers I have defined some booleans to indicate if
the functions have been overloaded and should been used.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top