Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 12th, 2006, 6:28 AM   #1
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 769
Rep Power: 3 Jimbo is on a distinguished road
Function pointer to a member function

I'm writing Chess in what little free time I have these days, and I'm currently working on getting the pieces to calculate their possible moves. My specific problem is with pawns, which can only move in a single direction, determined by where they started (i.e. by color). Anyways, I'm trying to create a forward pointer that will get the square "in front" of where the pawn is now.

I have the line in pawn.cpp:
Square (*fwd)() = (getColor() == White) ? &Square::up : &Square::down;
where Square::up() and Square::down() are public functions declared in square.h as
public: /* ... */
     Square* up();
     Square* down();
The error I get is:
Quote:
pawn.cpp:16: error: cannot convert `Square*(Square:)()' to `Square (*)()' in initialization
Is there a good way to get this to work? For that matter, what exactly does this error message mean? I'm thinking that it's complaining about making a pointer to a function specifically inside the Square class. Should I perhaps rethink how I'm doing this?

BTW, I'm compiling using g++ and running Linux, not that it should matter for this...
Jimbo is offline   Reply With Quote
Old May 12th, 2006, 7:38 AM   #2
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
What do you mean by forward pointer?

Square (*fwd)() = (getColor() == White) ? &Square::up : &Square::down;
I believe you are saying that you are assigning a pointer to function returning Square, to a pointer to a Square. I think that's the problem you are having.
I'm not sure as I haven't done function pointers in a while but here goes anyway:
Square* (Square::*fwd)() = (getColor() == White) ? &Square::up : &Square::down;
Should fix the errors, I haven't thought about what it would solve though.

I'd be hesitant on the function pointer in the first place. It's almost always possible to get the same functionality in a different manner. Oh and don't forget the following either:
Quote:
Originally Posted by wikipedia
Unlike other pieces, the pawn does not capture in the same way as it moves. A pawn captures diagonally, one square forward and to the left or right. Any piece directly in front of a pawn, friend or foe, blocks its advance.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates

Last edited by nnxion; May 12th, 2006 at 7:56 AM. Reason: missed colons
nnxion is offline   Reply With Quote
Old May 12th, 2006, 7:47 AM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Missing an asterisk.
Quote:
Originally Posted by MSDN
A pointer to a member of a class differs from a normal pointer because it has type information for the type of the member and for the class to which the member belongs. A normal pointer identifies (has the address of) only a single object in memory. A pointer to a member of a class identifies that member in any instance of the class. The following example declares a class, Window, and some pointers to member data.
class Window
{
public:
    Window();                               // Default constructor.
    Window( int x1, int y1,                 // Constructor specifying
        int x2, int y2 );                   //  window size.
    BOOL SetCaption( const char *szTitle ); // Set window caption.
    const char *GetCaption();               // Get window caption.
    char *szWinCaption;                     // Window caption.
};

// Declare a pointer to the data member szWinCaption.
char * Window::* pwCaption = &Window::szWinCaption;
In the preceding example, pwCaption is a pointer to any member of class Window that has type char*. The type of pwCaption is char * Window:. The next code fragment declares pointers to the SetCaption and GetCaption member functions.
const char * (Window::*pfnwGC)() = &Window::GetCaption;
BOOL (Window::*pfnwSC)( const char * ) = &Window::SetCaption;
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old May 12th, 2006, 5:53 PM   #4
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 769
Rep Power: 3 Jimbo is on a distinguished road
Thanks for the replies, that seems to have fixed it.
Jimbo is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:59 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC