Thread: Getting segfault using member-function pointers

  1. #1
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391

    Getting segfault using member-function pointers

    I'm not sure why I'm getting a segfault in this very simple code. I'm just playing around trying to get a feel for member function pointers right now:
    Code:
    #include <iostream>
    
    typedef unsigned char uchar;
    typedef unsigned int uint;
    
    struct AXS_CONTROL{
    	uint viewCostInfo(uint);
    	uint viewHarvestInfo();
    };
    
    typedef uint(AXS_CONTROL::*cost)(uint);
    typedef uint(AXS_CONTROL::*harvest)();
    
    uint AXS_CONTROL::viewCostInfo( uint n ){
    	std::cout << "Viewing cost information: " << n << ".\n";
    	return n;
    }
    
    uint AXS_CONTROL::viewHarvestInfo(){
    	std::cout << "Viewing harvest information.\n";
    	return 1;
    }
    
    #define CALL_MEMBER_FN(object,ptrToMember)  ((object).*(ptrToMember)) 
    
    int main(){
    
    	AXS_CONTROL axs;
    	cost cst;
    	uint ans = CALL_MEMBER_FN(axs,cst)(5);
    
    	return 0;
    }
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It seems that you haven't initialized cst.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Thank you kindly.
    Code:
    cost cst = &AXS_CONTROL::viewCostInfo;
    works perfect now
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  4. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM