Я написал простой класс, который выполняет основные арифметические операции, используя метод, который получает индекс и два значения для вычисления.Массив указателей функций C++
Индекс указывает, какую операцию следует выполнять в таблице, содержащей указатели на функции.
Вот мой код:
#include <iostream>
using namespace std;
class TArith
{
public:
static const int DIV_FACTOR = 1000;
typedef int (TArith::*TArithActionFunc)(int,int);
struct TAction
{
enum Values
{
Add,
Sub,
Mul,
Div,
count,
};
};
int action(TAction::Values a_actionIdx, int a_A, int a_B)
{
return (this->*m_actionFcns[a_actionIdx])(a_A,a_B);
}
private:
int add(int a_A, int a_B)
{
return a_A + a_B ;
}
int sub(int a_A, int a_B)
{
return a_A - a_B ;
}
int mul(int a_A, int a_B)
{
return a_A * a_B ;
}
int div(int a_A, int a_B)
{
return (0 != a_B) ? (DIV_FACTOR*a_A)/a_B : 0;
}
static TArithActionFunc m_actionFcns[TAction::count];
int m_a;
int m_b;
};
TArith:: TArithActionFunc TArith:: m_actionFcns[TAction::count] = {
TArith::add,
TArith::sub,
TArith::mul,
TArith::div
};
void main(void)
{
TArith arithObj;
int a=100;
int b=50;
for(int i = 0 ; i <TArith::TAction::count ; ++i)
{
int k = (i == (int)TArith::TAction::Div) ? TArith::DIV_FACTOR : 1;
cout<<arithObj.action((TArith::TAction::Values)i,a,b)/k<<endl;
}
cout<<endl;
}
Компилятор говорит:
'TArith::add': function call missing argument list; use '&TArith::add' to create a pointer to member
'TArith::sub': function call missing argument list; use '&TArith::sub' to create a pointer to member
'TArith::mul': function call missing argument list; use '&TArith::mul' to create a pointer to member
'TArith::div': function call missing argument list; use '&TArith::div' to create a pointer to member
Пробовали ли вы делать то, что говорит компилятор? – NathanOliver
Серьезно, компилятор дает вам ответ * прямо в сообщении об ошибке. * – Angew