Originally Posted by
phantomotap
You need to read that thread he linked a few more times.
Soma
I've read it. The problem is different. My vector is not the source of the problem The problem is how to use the member function pointers.
I accept your statement that "You can't use a member function without an object. You can't use a pointer to a member function without an object."
But if I modify the next-to-last line of my code just above (post # ) to similar to the way you might use an ordinary (non-member) function pointer, I get an error
Code:
error: 'fptr' undeclared (first use this function)
which I suppose makes sense because f does not have a member called 'fptr'. But if I change it to
Code:
sum = f.(*fptr)(x,y)
I get an error
Code:
error: syntax error before '*' token
error: invalid use of 'unary *' on pointer to member
There is no error associated with the line "fptr = *funcs.begin();
and this is interesting... If I modify the code as follows, it compiles and runs, but fails at the FIRST assert - although the types are consistent, the functions constructor doesn't put anything into the vector.
Code:
// fn_pointer3.cpp MODIFIED
#include <iostream>
#include <vector>
#include "funcs3.h"
#include <cassert>
using namespace std;
int main () {
// vector<int (*)(int,int)> funcs;
vector<int (functions::*)(int,int)> funcs;
functions f(funcs);
assert(!funcs.empty());
int x,y,op,sum;
cout << "1. add\n2. multiply\n\tChoose 1 or 2\n";
cin >> op;
cout << "enter 2 operands:\n";
cin >> x >> y;
int (functions::*fptr)(int,int);
fptr = *funcs.begin();
assert (fptr == &functions::add);
// sum = f.(*fptr)(x,y);
// cout << "the result is " << sum << "\n";
}
(using the same funcs3.h and funcs3.cpp as in post #11)
Anyway, this seems to be going nowhere. Function objects are not the same thing as pointers to member functions. Don't the unit test libraries that laserlight mentioned use pointers to member functions? Or not? I wouldn't expect that all classes that are tested by those tools must have each function in its own class.