在Android 中 ,如果一个模板类 A 继承与 模板类 B ,当A 引用B 的共有成员时, 如果不加 “this->” , 会产生“was not declared in this scope”错误
例如:
template <typename T> struct B {
int m;
int n;
int f ();
int g ();
};
int n;
int g ();
template <typename T> struct C : B<T> {
void h ()
{
m = 0; // error
f (); // error
n = 0; // ::n is modified
g (); // ::g is called
}
};
真确的写法是 :
template <typename T> void C<T>::h ()
{
this->m = 0;
this->f ();
this->n = 0
this->g ();
}
官方解释是 :http://gcc.gnu.org/gcc-3.4/changes.html “
”In a template definition, unqualified names will no longer find members of a dependent base (as specified by [temp.dep]/3 in the C++ standard)
2. another error : "expected unqualified-id before numeric constant"
用GCC 编译时 , 有时会遇见这个错误信息, 这个错误的可能原因是 代码中的变量 有重复的定义。