I have a class that hold public static method corresponding to mathematical operations. The class is implemented like such
class test
{
public:
test(){};
~test(){};
typedef int(test::*calcul)(int a,int b);
/*STATIC METHOD*/
static int add(int a, int b);
static int divide(int a, int b);
static int multiply(int a, int b);
static int substract(int a, int b);
static calcul _calcul[128];
private:
/* data */
};
I also added an array of pointer to function so that I can store these functions into that array and use them for later. However I have an error when I try to initialize the static array in the implementation file. Here is the error I get:
test.cpp:34:14: error: conflicting declaration ‘int (test::* test::_calcul [43])(int, int)’
34 | test::calcul test::_calcul['+'] = test::add;
| ^~~~
test.cpp:15:23: note: previous declaration as ‘int (test::* test::_calcul [128])(int, int)’
15 | static calcul _calcul[128];
To initialize one of the indexex of the array here what I wrote:
test::calcul test::_calcul['+'] = test::add;
My assumption of the above code sample is that the _calcul array at index '+' will be initialized with the function pointer of the add static method however it is not what is actually happening.
Here is the full program:
#include <cstdlib>
#include <iostream>
class test
{
public:
test(){};
~test(){};
typedef int(*calcul)(int a,int b);
/*STATIC METHOD*/
static int add(int a, int b);
static int divide(int a, int b);
static int multiply(int a, int b);
static int substract(int a, int b);
static calcul _calcul[128];
private:
/* data */
};
/*----------------------------------STATIC MEMBER FUNCTION----------------------------------*/
int test::add(int a, int b){return a + b;};
int test::substract(int a, int b){return a - b;};
int test::multiply(int a, int b){return a * b;};
int test::divide(int a, int b)
{
if (b == 0)
{
std::cerr << "Error\n";
exit(1);
}
return a / b;
};
/*----------------------------------STATIC MEMBER FUNCTION----------------------------------*/
test::calcul test::_calcul['+'] = test::add;
int main(void)
{
test d;
}