I have a program that deals with operations in polynomials. An “operator” class has a char pointer to the input string, a pointer to a function of type float (float, float) that is used to perform an operation on the arguments. Then there are a series of methods, setSymb(char *) points opChar to the argument, and points the opFunc pointer to the function that corresponds to the operator character. However when I run this method, and at the line where it assigns the function to the pointer function, there is an unhandled exception under new.cpp, when it tries to allocate memory of some sort. This confuses me a great deal.
#include "stckArrClassLib.h"
using namespace stacks;
using namespace conversion;
#define SIDE bool
#define LEFT 0
#define RIGHT 1
class oprt
{
public:
typedef float (oprt::*fp)(float, float);
private:
char *opSymb;
fp opFunc;
float *lOpnd;
float *rOpnd;
oprt *Lnext;
oprt *Rnext;
float rslt;
protected:
float add(float a, float b){return a+b;};
float mult(float a, float b){return a*b;};
float div(float a, float b){return a/b;};
float exp(float a, float b){return powf(a, b);};
float par(float a, float b){return a;};
public:
oprt (void) {opFunc=NULL;};
oprt &setSymb(char *);
oprt &setNext(SIDE, oprt *);
oprt &setOpnd(SIDE, float);
float evaluate();
char *getSymb();
fp getFunc();
float *getOpnd(SIDE);
oprt *getNext(SIDE);
};
oprt& oprt::setSymb(char *set)
{
CArray<char, 8> opStr;
opStr[0]=*(set), set;
int i;
for(i=1; i<7 && (!(set[i]>='0' && set[i]<='9') && !(set[i]=='+' || set[i]=='-' || opStr[i]=='*' || set[i]=='/' || set[i]=='^' || set[i]=='(')); i++)
opStr[i]=set[i];
opStr[i]=0;
if(strcmp((const char *)opStr.arr, "+")==0)
//problem is here
opFunc=&oprt::add;
else if(strcmp((const char *)opStr.arr, "*")==0)
//and here
opFunc=&oprt::mult;
else if(strcmp((const char *)opStr.arr, "/")==0)
//etc.
opFunc=&oprt::div;
else if(strcmp((const char *)opStr.arr, "^")==0)
opFunc=&oprt::exp;
else if(strcmp((const char *)opStr.arr, "(")==0 || strcmp((const char *)opStr.arr, ")")==0)
opFunc=&oprt::par;
return *this;
}
oprt& oprt::setNext(SIDE side, oprt *next)
{
if(side==LEFT)
{
Lnext=next;
lOpnd=&(next->rslt);
}
else if(side==RIGHT)
{
Rnext=next;
rOpnd=&(next->rslt);
}
return *this;
}
float oprt::evaluate()
{
return rslt=(this->*opFunc)(*getOpnd(LEFT), *getOpnd(RIGHT));
}
char *oprt::getSymb()
{
return opSymb;
}
oprt::fp oprt::getFunc()
{
return opFunc;
}
float *oprt::getOpnd(SIDE side)
{
if(!((side==LEFT)? (Lnext==this):(Rnext==this)))
(side==LEFT? Lnext:Rnext)->evaluate();
return (side==LEFT)? (lOpnd):(rOpnd);
}
oprt *oprt::getNext(SIDE side)
{
return (side==LEFT)? (Lnext):(Rnext);
}
oprt& oprt::setOpnd(SIDE side, float set)
{
(side==LEFT)? (*lOpnd=set):(*rOpnd=set);
return *this;
}
Here is the excerpt of new.cpp which is called at opFunc=&oprt::add in the function oprt& oprt::setSymb(char *)
extern "C" void * __cdecl _nh_malloc_dbg (
size_t nSize,
int nhFlag,
int nBlockUse,
const char * szFileName,
int nLine
)
{
int errno_tmp = 0;
void * pvBlk = _nh_malloc_dbg_impl(nSize, nhFlag, nBlockUse, szFileName, nLine, &errno_tmp);
if ( pvBlk == NULL && errno_tmp != 0 && _errno())
{
errno = errno_tmp; // recall, #define errno *_errno()
}
return pvBlk;
}
The error is called when malloc is called.
I think you have to modify the typedef to this:
You also don’t want to ommit the ‘&’ ampersand when assigning: