Reputation: 121
I'm new to c++, and I'm struggling with the following code. I keep getting errors by the starred lines. I've done some research on why it isn't working but haven't found anything helpful. Can you see any problems, and explain what I should do to fix this?
// FunctionArray.cpp : Defines the entry pofloat for the console application.
//
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
class MathPrg{
public:
MathPrg(float num1, float num2)
{
num1 = num1;
num2 = num2;
int choice = presentChoices();
getAnswer(choice, num1, num2);
}
void getAnswer(int choice,float num1, float num2){
typedef float (MathPrg::*f[4]) (float, float);
**f set = {add, subtract, multiply, divide};
float answer = (*set[choice])( num1, num2 );**
cout<< answer;
}
float add(float num1, float num2){
return num1+num2;}
float subtract(float num1, float num2){
return num1-num2;}
float multiply(float num1, float num2){
return num1*num2;}
float divide(float num1, float num2){
return num1/num2;}
int presentChoices(){
cout<<"Enter 0 to add \nEnter 1 to subtract \nEnter 2 to multiply\nEnter 3 to divide";
int choice;
cin>> choice;
return choice;
}
};
float _tmain(float argc, _TCHAR* argv[])
{
float num1, num2;
cout<<"Enter two numbers";
cin>> num1>> num2;
MathPrg mathP(num1, num2);
cin>>num1;
return 0;
}
errors: ------ Build started: Project: FunctionArray, Configuration: Debug Win32 ------ FunctionArray.cpp c:\documents and settings\chaya\my documents\visual studio 2010\projects\functionarray\functionarray\functionarray.cpp(22): error C4867: 'MathPrg::add': function call missing argument list; use '&MathPrg::add' to create a pointer to member c:\documents and settings\chaya\my documents\visual studio 2010\projects\functionarray\functionarray\functionarray.cpp(22): error C4867: 'MathPrg::subtract': function call missing argument list; use '&MathPrg::subtract' to create a pointer to member c:\documents and settings\chaya\my documents\visual studio 2010\projects\functionarray\functionarray\functionarray.cpp(22): error C4867: 'MathPrg::multiply': function call missing argument list; use '&MathPrg::multiply' to create a pointer to member c:\documents and settings\chaya\my documents\visual studio 2010\projects\functionarray\functionarray\functionarray.cpp(22): error C4867: 'MathPrg::divide': function call missing argument list; use '&MathPrg::divide' to create a pointer to member c:\documents and settings\chaya\my documents\visual studio 2010\projects\functionarray\functionarray\functionarray.cpp(23): error C2171: '*' : illegal on operands of type 'float (__thiscall MathPrg::* )(float,float)' c:\documents and settings\chaya\my documents\visual studio 2010\projects\functionarray\functionarray\functionarray.cpp(23): error C2064: term does not evaluate to a function taking 2 arguments ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Upvotes: 0
Views: 393
Reputation: 2337
void getAnswer(int choice,float num1, float num2){
typedef float (MathPrg::*f) (float, float);
f set[] = {&MathPrg::add, &MathPrg::subtract, &MathPrg::multiply, &MathPrg::divide};
float answer = (this->*set[choice])( num1, num2 );
cout << answer;
}
works fine for me.
You must assign the address of class's method (&MathPrg::add
) and then call it on the specified object/instance - (this->*set[choice])( num1, num2 )
,
Upvotes: 2