वाह। सी ++ कभी भी मुझे अजीबता से आश्चर्यचकित नहीं करता है।
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). For example,
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
}
};
You must make the names dependent, e.g. by prefixing them with this->. Here is the corrected definition of C::h,
template <typename T> void C<T>::h()
{
this->m = 0;
this->f();
this->n = 0
this->g();
}
As an alternative solution (unfortunately not backwards compatible with GCC 3.3), you may use using declarations instead of this->:
template <typename T> struct C : B<T> {
using B<T>::m;
using B<T>::f;
using B<T>::n;
using B<T>::g;
void h()
{
m = 0;
f();
n = 0;
g();
}
};
कि सिर्फ पागल के सभी प्रकार है। धन्यवाद, डेविड।
यहाँ "temp.dep/3" के मानक खंड है [आईएसओ/आईईसी 14882: 2003]:
In the definition of a class template or a member of a class template, if a base class of the class template depends on a template-parameter, the base class scope is not examined during unqualified name lookup either at the point of definition of the class template or member or during an instantiation of the class template or member. [Example:
typedef double A;
template<class T> class B {
typedef int A;
};
template<class T> struct X : B<T> {
A a; // a has typedouble
};
The type name A
in the definition of X<T>
binds to the typedef name defined in the global namespace scope, not to the typedef name defined in the base class B<T>
. ] [Example:
struct A {
struct B { /* ... */ };
int a;
int Y;
};
int a;
template<class T> struct Y : T {
struct B { /* ... */ };
B b; //The B defined in Y
void f(int i) { a = i; } // ::a
Y* p; // Y<T>
};
Y<A> ya;
The members A::B
, A::a
, and A::Y
of the template argument A
do not affect the binding of names in Y<A>
. ]
देखें [एक टेम्पलेटेड व्युत्पन्न कक्षा में, मुझे सदस्य वर्ग के अंदर "यह->" के साथ बेस क्लास सदस्य नामों को अर्हता प्राप्त करने की आवश्यकता क्यों है?] (Http://stackoverflow.com/questions/7908248/in-a- templated-derived-class-why-do-i-need-to-qualify-base-class-सदस्य-names-w) – curiousguy