error: address of overloaded function with no contextual type information

不想你离开。 提交于 2020-01-13 19:47:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!