Hi!
I am trying to use a function pointer array in a class but its not working. I tried doing exactly the same thing without a class here and it works fine:
But when i try do the same thing using a class test 3 like this:
Clearly i am doing something wrong. Could someone please tell me what i am doing wrong or how i can use function pointer arrays in this manner, thanks!
I am trying to use a function pointer array in a class but its not working. I tried doing exactly the same thing without a class here and it works fine:
Rich (BB code):
#include <iostream>
using namespace std;
void function1(int &a, int &b);
void function2(int &a, int &b);
void function3(int &a, int &b);
void function4(int &a, int &b);
int main()
{
int number1 = 1, number2 = 2;
function4(number1,number2);
return 0;
}
void function1(int &a, int &b)
{
cout << "This is function 1 printing out the value of a and b: " << a << b << endl;
}
void function2(int &a, int &b)
{
cout << "This is function 2 printing out the value of a and b: " << a << b << endl;
}
void function3(int &a, int &b)
{
cout << "This is function 3 printing out the value of a and b: " << a << b << endl;
}
void function4(int &a, int &b)
{
int number = 2;
void (*ptr[4])(int &, int &) = {function1,function2,function3};
(*ptr[number])(a,b);
}
Rich (BB code):
#include <iostream>
using namespace std;
class test3
{
public:
void function1(int &a, int &b);
void function2(int &a, int &b);
void function3(int &a, int &b);
void function4(int &a, int &b);
};
int main()
{
int number = 2,number1 = 3,number2 = 5;
test3 test;
test.function4(number,number1);
return 0;
}
void test3::function1(int &a, int &b)
{
cout << "This is function 1 printing out the value of a and b: " << a << b << endl;
}
void test3::function2(int &a, int &b)
{
cout << "This is function 2 printing out the value of a and b: " << a << b << endl;
}
void test3::function3(int &a, int &b)
{
cout << "This is function 3 printing out the value of a and b: " << a << b << endl;
}
void test3::function4(int &a, int &b)
{
int number = 2;
void (*ptr[4])(int &, int &) = {test3::function1,test3::function2,test3::function3};
(*ptr[number])(a,b);
}
Rich (BB code):
I get the error message:
1>------ Build started: Project: test3, Configuration: Debug Win32 ------
1>Compiling...
1>Test3.cpp
1>z:\college\year2\programming\programs\test3\test3\test3.cpp(39) : error C2440: 'initializing' : cannot convert from 'overloaded-function' to 'void (__cdecl *)(int &,int &)'
1> None of the functions with this name in scope match the target type
1>z:\college\year2\programming\programs\test3\test3\test3.cpp(39) : error C2440: 'initializing' : cannot convert from 'overloaded-function' to 'void (__cdecl *)(int &,int &)'
1> None of the functions with this name in scope match the target type
1>z:\college\year2\programming\programs\test3\test3\test3.cpp(39) : error C2440: 'initializing' : cannot convert from 'overloaded-function' to 'void (__cdecl *)(int &,int &)'
1> None of the functions with this name in scope match the target type
1>Build log was saved at "file://z:\College\Year2\Programming\Programs\test3\test3\Debug\BuildLog.htm"
1>test3 - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Last edited: