I am trying to learn how to use the MFC in Visual C++ 2008 Professional. I have Ivor Horton's
Beginning Visual C++ 2008 and I successfully implemented the Sketcher example in that book. As a small challenge to myself, I tried to add to Sketcher the capability to plot simple math functions, like sin(t), cos(t), exp(t) and log(t). I have run into trouble compiling with the "error C2065: 'm_pFunction' : undeclared identifier" where m_pFunction is a pointer to the function to be plotted. The complete Sketcher code is too lengthy to include here. It is as presented by Horton with the exception of the code I added for plotting the function. I'll sketch essentials of the additions here which show where the problem lies.
Horton declares the elements to be sketched in the file Elements.h. There he declares the class CElement which is the base for all the other elements as follows:
Code:
class CElement : public CObject
{
DECLARE_SERIAL(CElement)
protected:
COLORREF m_Color; // Color of an element
CRect m_EnclosingRect;
int m_Pen; // Pen width
CPoint m_StartPoint;
CPoint m_EndPoint;
double (*m_pFunction)(double); // Pointer to function
//static double (*m_pFunction)(double); // Pointer to function
public:
// double (*m_pFunction)(double) = sin; // Pointer to function
virtual ~CElement();
// Virtual Draw operation
virtual void Draw(CDC* pDC, CElement* pElement = 0) {}
virtual void Move(CSize& aSize) {} // Move an element
CRect GetBoundRect(); // Bounding rectangle element
virtual void Serialize(CArchive& ar);//Serialize function for the class
protected:
CElement(); // Here to prevent it being called outside the class
};
The references to m_pFunction are my additions. The commented out references to m_pFunction are my failed experiments.
In Elements.h I declare the class CPlot as follows:
Code:
class CPlot : public CElement
{
public:
// Function to display plot element
virtual void Draw(CDC* pDC, CElement* pElement = 0);
// double (*m_pFunction)(double); // Pointer to function
CPlot(double (*pt2Func)(double), CPoint Start, CPoint End, COLORREF aColor, int PenWidth);
virtual void Move(CSize& aSize);
protected:
// double (*m_pFunction)(double); // Pointer to function
CPlot(void);
~CPlot(void);
CList<CPoint, CPoint&> m_PlotPointList; // Type safe point list
public:
// Set pointer to function to plot
void SetFunction(double (*pt2Func)(double t));
double CPlot::GetFunction();
};
In SketcherView.cpp I create a CPlot object as follows:
Code:
return new CPlot(m_pFunction, m_FirstPoint, m_SecondPoint,
pDoc->GetElementColor(), pDoc->GetPenWidth());
When I try to compile with the above code I get the C2065 errors. However, if I replace the above m_pFunction declaration with the following line placed before the CElement declaration (declaring m_pFunction as a static global variable):
Code:
static double (*m_pFunction)(double) = sin; // Pointer to function
the program compiles and runs OK!
If anyone can shed any light on this I would be very appreciative.
Thank you.
Ted