Name lookup, templates, and accessing members of base classes

如下的小程序:

 

#include <iostream> using namespace std; template <typename T> struct Base { int f(); }; template <typename T> struct Derived : Base<T> { int g() { return f(); }; }; int main() { return 0; }

 

使用g++4.2,编译结果:

 

x.cc: In member function `int Derived<T>::g()': x.cc:6: error: there are no arguments to `f' that depend on a template parameter, so a declaration of `f' must be available x.cc:6: error: (if you use `-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)

 

具体原因参考: http://gcc.gnu.org/onlinedocs/gcc/Name-lookup.html

 

说明:使用g++3.2 以及 CC(5.0)是可以编译过的。

 

解决方法:

1.  using Base<T>::f;

2. f => this->f

 

推荐使用第二种方法。

你可能感兴趣的:(struct,function,templates,deprecated)