1 Replies - 859 Views - Last Post: 12 February 2009 - 04:49 AM Rate Topic: -----

#1 redbloodcell  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 10-February 09

problem with arrays of pointers to member functions within class

Posted 11 February 2009 - 07:03 PM

hi, initially my code was working fine withotu implementing the pointer array. now, after writing the array of pointers, i get this error:
error C3867: 'Math::addition': function call missing argument list; use '&Math::addition' to create a pointer to member

#include "stdafx.h"
#include <stdio.h> //required for getchar()

class Math 
{
public:
//num1, num2 and remainder are public data members of the Math class
//addition, subtraction, multiplication and division are member functions, publicly accessible
	int num1; 
	int num2;
	int remainder; //required for division function

	int addition( Math* pMath ) //these function takes arguments in the form of pointers, and the pointer will be a pointer to an object of the Math class
	{
		return pMath->num1 + pMath->num2; //returns sum of num1 and num2 (pmath->num1 is equivalent to (*pMath).num1)
	}

	int subtraction( Math* pMath ) //returns difference of num1 and num2
	{
		return pMath->num1 - pMath->num2;
	}

	int multiplication( Math* pMath ) //returns product of num1 and num2
	{
		int sign = 1;
		if( pMath->num2 < 0 ) { pMath->num2 = -pMath->num2; sign = -1; }	//obtains |num2|, sets sign to -1 if necessary
		int ans = 0;
		//adds num1 to itself for num2 amount of times (num2 needs to be positive)
		for( int i=0; i< pMath->num2; i++ )
			ans += pMath->num1;
		return ans*sign;
	}

	int division( Math* pMath )
	{
		//attain absolute value of denominator, and set signBot to -1 if necessary.
		int signBot = 1; //sign of denominator
	//obtains |num2|, from this point onwards num2 is positive. If num2 is negative, signBot = -1 
		if( pMath->num2 < 0 ) { pMath->num2 = -pMath->num2; signBot = -1; }
		
		int quotient = 0;
		remainder = pMath->num1; //initialize remainder as num1, ie. dividend
		

		//case (1) if divided by zero:
		if( pMath->num2 == 0 )
		{
			printf("\nThe answer is infinity.\n");
			remainder = 0;
			return 0;
		}
		
		//case (2) numerator is zero
		if( pMath->num1 == 0 ) { remainder = 0; return 0; }
		
		//obtain |num1|, num1 is positive from this point onwards. If num1 is negative, then signTop = -1.
		int signTop = 1;
		if(pMath->num1 < 0 ) { pMath->num1 = - pMath->num1; signTop = -1; }
		
		//if |num1| < |num2|, result is fraction. Quotient is zero, remainder = pMath->num1 (already initialized)
		if(pMath->num1 < pMath->num2 )
		{
			remainder *= signTop; //restore original sign of numerator

			return 0; //quotient = 0
		}
		
		//if |num1| > |num2|, proceed to while loop.
		while(remainder >= pMath->num2 ) 
		{
			//increments quotient by 1 each time the divisor is subtracted from dividend
			++quotient;
			remainder -= pMath->num2; //initially remainder = pMath->num1
		}
//restore quotient and remainder to correct signs
		remainder *= signTop*signBot; 
		return quotient*signTop*signBot;
	}
	
	double power (Math* pMath)
	{
	double num1;
	int num2;

	num1 = (double)pMath->num1; // To allow for decimal values should a reciprocal be needed.
	num2 = pMath->num2;		// To reduce typing time...

	if (num2 < 0) //if exponent is negative, x^-y = (1/x)^y
	{
		num1 = 1.0 / num1;
		num2 = -num2;
	}

	double ans = 1;
	for (int count = 1; count < num2; count++)
	{
		ans *= num1;
	}

	return ans;

	}

	int factorial (Math* pMath)
	{
		if (pMath->num1 < 0)
		{printf ("Negative factorials are undefined!\n");
		return 0;}
		int ans = 1;
		for (int i=2; i<=pMath->num1; i++) // multiplies 2x3x4...xnum1
		ans *= i; 

		return ans;
	}

};// end of Math class

// gets values for num1 and num2 ...
void getNumbers( int& i, int& j, int index )
{
	// array of strings to select proper phrasing based on user choice
	char* opname[]={"add", "subtract", "multiply", "divide by", "raise to the power of"};
	printf ("Please enter a number: ");
	do scanf( "%d", &i );
	while( getchar() != '\n' ); // proceed until enter key is pressed
	if (index != 6) //only obtain j if not factorial
	{
		printf ( "Please enter another number to %s: ", opname[index-1] );
		do scanf( "%d", &j );
		while( getchar() != '\n' ); 
	}// proceed until enter key is pressed
}

int _tmain(int argc, TCHAR *argv[], TCHAR *envp[]) /////////////////////// MAIN ////////////////////////////////////////
{
	Math op;
	// pMath to address of op, object of Math class
	Math* pMath = &op;
	int (*pfunc[])(Math*)=(op.addition, op.subtraction, op.multiplication, op.division, op.factorial);
	
	
	// these references will be arguments to the getNumbers function
	// and will be modified accordingly
	int& i = op.num1;
	int& j = op.num2;

	for(;; ) // forever loop ... until 'return 0;' called in main ...
	{
		printf
		(
			"Welcome to my calculator. Your choices are ...\n"
			"1: Addition\n"
			"2: Subtraction\n"
			"3: Multiplication\n"
			"4: Division\n"
			"5: Power\n"
			"6: Factorial\n"
			"7: Exit\n"
			"Please enter a number in the range of 1 to 6, or 7 to quit: "
		);

		int choice = -1;
		do scanf( "%d", &choice );
		while( getchar() != '\n' ); 
		
		if( choice >= 1 && choice <= 6 )
			getNumbers( i, j, choice );	// don't call this if INVALID choice

		 // prints answer according to choice
		if( choice == 1 )
			printf( "Answer: %d\n\n", pfunc[0](pMath) );
		else if( choice == 2 )
			printf( "Answer: %d\n\n", pfunc[1](pMath) );
		else if( choice == 3 )
			printf( "Answer: %d\n\n", pfunc[2](pMath) );
		else if( choice == 4 )
		{
			printf( "Quotient: %d\n", pfunc[3](pMath) );
			printf( "Remainder: %d\n\n", op.remainder );
		}
		else if( choice == 5 )
			printf( "Answer: %f\n\n", op.power(pMath) );
		else if( choice == 6 )
			printf( "Answer: %d\n\n", pfunc[4](pMath) );
		else if( choice == 7 )
			return 0;
			
		else
			printf( "This choice %d is invalid.\n\n", choice );
	}
} 





Is This A Good Question/Topic? 0
  • +

Replies To: problem with arrays of pointers to member functions within class

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,898
  • Joined: 25-October 06

Re: problem with arrays of pointers to member functions within class

Posted 12 February 2009 - 04:49 AM

you can define your array of pointers to member functions so
// define pointer to a MATH member function
typedef  int (Math::*MathMemFn)(Math::Math*);
// macro to call a Math member function
#define CALL_MEMBER_FN(object,ptrToMember)  ((object).*(ptrToMember))

int main() /////////////////////// MAIN ////////////////////////////////////////
{
	Math op;
	// pMath to address of op, object of Math class
	Math* pMath = &op;
	MathMemFn pfunc[]={&Math::addition, &Math::subtraction, &Math::multiplication, &Math::division, &Math::factorial};


and you would call a function so
		if( choice == 1 )
			printf( "Answer: %d\n\n", CALL_MEMBER_FN(op, pfunc[0])(pMath) );


for more details of pointers to member functions see
http://www.parashift...to-members.html
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1