错误:没有上下文类型信息的重载函数的地址 [英] error: address of overloaded function with no contextual type information
问题描述
代码:
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;
}
我想将第2行替换为第3行("if"语句替换为?:").
如果我取消注释3,则编译器会给我一个错误.
如果我删除第1行,则第3行有效.
问题是:编译器向我要什么?
错误:错误:没有上下文类型信息的重载函数的地址
编译器:gcc-4.5.2
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
推荐答案
错误:没有上下文类型信息的重载函数的地址
error: address of overloaded function with no contextual type information
有两个具有operator++
名称的函数(即消息的重载函数"位),您需要指定所需的函数(即上下文类型信息"之一):
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
您必须考虑到上面的子表达式独立于封闭的完整表达式,即对go
的赋值.因此,它必须本身是正确的,即它不能使用go
的类型来选择正确的重载,因为它不是此特定子表达式的一部分.
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.
这篇关于错误:没有上下文类型信息的重载函数的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!