#include <iostream>
template<class T> struct A {
typedef T a;
};
template<class T>
struct B {
typedef typename A<T>::a a;
static a foo(a b);
};
template<class T>
a B<T>::foo(a b) {return b}
int main() {
std::cout << B<int>::foo(1);
}
给出以下错误:(try it)。模板类中成员函数的C++名称解析
main.cpp:13:1: error: 'a' does not name a type
a B<T>::foo(a b) {return b}
内联定义不会遇到此错误。
有人可以请解释为什么编译器无法解决a
在这种情况下,我怎么能使这个代码工作。
我想不能解决所有的名称明确就像
typename B<T>::a B<T>::foo(typename B<T>::a b) {return b}
,因为它会降低可读性。
http://stackoverflow.com/questions/1643035/propagating-typedef-from-based-to-derived-class-for-template and http://stackoverflow.com/questions/1567730/inheritance-and-templates -in-c-why-are-methods-invisible似乎是相关的。 – marc