Wrox Programmer Forums
|
BOOK: Ivor Horton's Beginning Visual C++ 2008 ISBN: 978-0-470-22590-5
This is the forum to discuss the Wrox book Ivor Horton's Beginning Visual C++ 2008 by Ivor Horton; ISBN: 9780470225905
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Ivor Horton's Beginning Visual C++ 2008 ISBN: 978-0-470-22590-5 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 6th, 2010, 12:46 PM
Registered User
 
Join Date: Nov 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Pointer-to-function problem in Visual C++ 2008

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
 
Old July 15th, 2010, 09:15 AM
Authorized User
 
Join Date: Jan 2010
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to meteormatt Send a message via Yahoo to meteormatt
Default

Quote:
Originally Posted by tedkurtz View Post
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
Thank you very much.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginning Visual C++ 2008 & Professional Visual C++ 2008 tarana BOOK: Beginning Microsoft Visual C# 2008 ISBN: 978-0-470-19135-4 3 March 26th, 2010 06:11 PM
Visual studio 2005(32 bit) code not work in visual studio 2008 on windows server 2008 gr8.jain Visual Basic 2008 Essentials 1 August 31st, 2009 10:07 AM
Function/Subroutine Pointer Parameter pjm Access VBA 3 October 3rd, 2006 09:07 AM
pointer-to-function in C++ jacob C++ Programming 2 October 23rd, 2004 05:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.