A
Anton
Hey all
I have a function, which I would like to get an arithmetic infix
operator (+,-,/,*) as an argument. I have reduced my problem to the
following lines of code:
The first cout-line in main works.
The second gets the following compiler error:
The third line gets another error:
Is passing an infix operator as argument doable or do I need the wrap
around used in the fiorst line of main?
Anton
Computer science student
I have a function, which I would like to get an arithmetic infix
operator (+,-,/,*) as an argument. I have reduced my problem to the
following lines of code:
#include <iostream>
using namespace std;
class MyClass{
public:
int test(int, int, int(*f)(int,int));
int add(int,int);
};
int MyClass::test(int i, int j, int (*func)(int,int)){
return func(i,j);
}
int add(int i, int j){return i+j;}
int main(){
MyClass mc;
// cout << mc.test(5,10,&add) << endl;
// cout << mc.test(5,10,&operator+) << endl;
// cout << mc.test(5,10,&(int:perator+)) << endl;
return 0;
}
The first cout-line in main works.
The second gets the following compiler error:
test.cpp: In function 'int main()':
test.cpp:24: error: no matching function for call to 'MyClass::test(int, int, <unresolved overloaded function type>)'
test.cpp:12: note: candidates are: int MyClass::test(int, int, int (*)(int, int))
The third line gets another error:
test.cpp: In function 'int main()':
test.cpp:25: error: expected primary-expression before 'int'
test.cpp:25: error: expected `)' before 'int'
Is passing an infix operator as argument doable or do I need the wrap
around used in the fiorst line of main?
Anton
Computer science student