问题
Code:
class que {
public:
que operator++(int) {} // 1
que &operator++() {}
que &operator+=(int n) {
que& (que::*go)();
go = 0; if(n > 0) go = &que::operator++ ; // 2
//go = (n > 0) ? (&que::operator++) : 0 ; // 3
}
};
int main() {
que iter;
iter += 3;
return 0;
}
I want to replace line 2 by line 3("if" statement for "?:").
If I uncomment 3, compiler gives me an error.
If I delete line 1, then line 3 works.
Question is: what does compiler want from me?
Error: error: address of overloaded function with no contextual type information
Compiler: gcc-4.5.2
回答1:
error: address of overloaded function with no contextual type information
There are two functions with the operator++
name (that's the 'overloaded function' bit of the message), you need to specify which one you want (that's the 'contextual type information' one):
n > 0 ? (que& (que::*)())&que::operator++ : 0
You have to consider that the above subexpression is independent from the enclosing full expression, the assignment to go
. So it must be correct on its own, i.e. it can't use the type of go
to pick the correct overload because it's not part of this particular subexpression.
来源:https://stackoverflow.com/questions/7121196/error-address-of-overloaded-function-with-no-contextual-type-information