#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