编译错误:未解析的重载函数类型 [英] Compile error: unresolved overloaded function type
问题描述
我尝试使用g ++ 4.7.2编译以下内容:
I try to compile the following with g++ 4.7.2:
template <typename T>
struct A {
struct B {
T t;
template<T B::*M>
T get() {
return this->*M;
}
};
B b;
T get() {
return b.get<&B::t>();
}
};
int main() {
A<int> a;
a.get();
}
它给我
test.cpp: In member function ‘T A<T>::get()’:
test.cpp:15:23: error: expected primary-expression before ‘)’ token
test.cpp: In instantiation of ‘T A<T>::get() [with T = int]’:
test.cpp:22:8: required from here
test.cpp:15:23: error: invalid operands of types ‘<unresolved overloaded function type>’ and ‘int A<int>::B::*’ to binary ‘operator<’
为什么?
谢谢。
推荐答案
您需要使用模板
disambiguator:
You need to use the template
disambiguator:
return b.template get<&B::t>();
没有它,解析表达式时:
Without it, when parsing the expression:
b.get<&B::t>();
编译器无法判断是否应该解释 get
作为成员变量的名称,后跟<
符号(小于),或作为成员函数模板的实例化 get
。
The compiler can't tell whether it should interpret get
as the name of a member variable followed by a <
sign (less-than), or as the instantiation of a member function template called get
.
虽然我们知道我们的表达式的含义是什么,编译器不能,至少不会在实例化之前发生,并且执行句法解析即使您的函数从未实例化。
Although we know what is the intended meaning of our expression, the compiler cannot, at least not before instantiation occurs - and syntactic parsing is performed even though your function is never instantiated.
这篇关于编译错误:未解析的重载函数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!