Derived class cannot see base class member [duplicate] - c++

This question already has answers here:
Derived template-class access to base-class member-data
(3 answers)
Closed 2 years ago.
What am I doing wrong in this code?
template <typename T>
class CLASS1
{
public:
T member;
};
template <typename T>
class CLASS2 : public CLASS1<T>
{public:
void func()
{
member = 4;
}
};
Visual Studio error:
member identifier not found
G++ error:
‘member’ was not declared in this scope

You need to specify where the name member comes from. In this case, it comes from the inherited class template CLASS1<T>, so you need to say:
void func()
{
CLASS1<T>::member = 4;
}
If you say this->member, then the compiler knows to look for names in the base classes as well. So you could do:
void func()
{
this->member = 4;
}

Related

Why the incomplete type can be new from the base class with template technique [duplicate]

This question already has an answer here:
CRTP -- accessing incomplete type members
(1 answer)
Closed 11 months ago.
The following code will fail
class InComplte;
void f() {
new InComplte();
}
class InComplte {
};
Because it is newing an incomplete type.
But if template is used, the following code can compile
template<typename T>
class CComCoClass {
public:
static void create_di(T**p) {
*p = new T();
}
};
class Book: public CComCoClass<Book> {
public:
int i = 3;
};
The base class CComCoClass seems to be newing an incomplete type.
Why the Book can inherit a base class that creates it?
A member function of a class template is not evaluated when the class template is instantiated. The functions instantiation is delayed until it is called. create_di isn't called in the example, so it isn't evaluated.

Sometimes class members are not inherited by subclasses [duplicate]

This question already has answers here:
Why do I have to access template base class members through the this pointer?
(3 answers)
Derived template-class access to base-class member-data
(3 answers)
Closed 5 years ago.
When we create a template class example1 which derives from another template class example2. The template class example1 does not inherit systematically of example2 members.
Consider the code bellow:
template<class T>
class example2 {
protected:
int a;
public:
int getA() const {return a;}
};
template<class T>
class example1 : public example2<T> {
T* p;
public:
void test() {
example2<T>::a = 8;
}
};
in the method test of the class example1, we have to use example2<T>:: operator in order to be able to access to the member a. Otherwise, the complier generates an error: 'a' was not declared in this scope.
I mean, why the complier does not recognize a in the class example1? Why we should specify example2<T>::a and not just a?
Thanks

how to inherit a class with templates? [duplicate]

This question already has answers here:
Accessing inherited variable from templated parent class [duplicate]
(2 answers)
Why do I have to access template base class members through the this pointer?
(3 answers)
Closed 9 years ago.
Why would the following work just fine:
class a
{
public:
int n;
};
class b : public a
{
public:
b()
{
n = 1;
}
};
int main()
{
}
but this does not work:
template <class T>
class a
{
public:
int n;
};
template <class T>
class b : public a<T>
{
public:
b()
{
n = 1;
}
};
int main()
{
}
and gives the following error:
1.cpp: In constructor ‘b<T>::b()’:
1.cpp:14: error: ‘n’ was not declared in this scope
and how would one inherit a template class while being able to use its base members and keep the type generic?
You need to qualify it with "this" or with a "using" directive (or explicitly with the base class qualification).
In a nutshell: that variable is a nondependent type (non-dependent on the T of the template class), the compiler doesn't look into a dependent type (your a< T >) when looking for declarations for a nondependent type.
this->n, since "this" refers to a dependent class, works. The same with the other methods I listed.
References:
Faq here: http://www.parashift.com/c++-faq-lite/nondependent-name-lookup-members.html
Live example here: http://ideone.com/nsw4XJ

inheritance with c++ templates [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Inheritance and templates in C++ - why are methods invisible?
Why can't I use variable of parent class that is template class?
I'm trying to use template with inheritance and try to
access public member of parent class from child class. But this is not
compiling with g++.
template<class T>
class A
{
public:
A<T>* t;
};
template<class E>
class D:public A<E>
{
public:
D()
{
t = 0;
}
};
int main()
{
D<int> d;
return 0;
}
compile error:
g++ Test.C Test.C: In constructor D<E>::D():
Test.C:27: error: 't' was not declared in this scope

"not declared in this scope" error with templates and inheritance [duplicate]

This question already has answers here:
Why do I have to access template base class members through the this pointer?
(3 answers)
accessing protected members of superclass in C++ with templates [duplicate]
(2 answers)
Closed 8 years ago.
Here is code sample which reproduces my problem:
template <typename myType>
class Base {
public:
Base() {}
virtual ~Base() {}
protected:
int myOption;
virtual void set() = 0;
};
template <typename InterfaceType>
class ChildClass : public Base < std::vector<InterfaceType> >
{
public:
ChildClass() {}
virtual ~ChildClass() {}
protected:
virtual void set();
};
template <typename InterfaceType>
void ChildClass<InterfaceType>::set()
{
myOption = 10;
}
My usage in main():
ChildClass<int> myObject;
I get the following error (gcc 4.4.3 on ubuntu):
‘myOption’ was not declared in this scope
If my ChildClass would be without new template parameter this would work fine, i.e.:
class ChildClass : public Base < std::vector<SomeConcreteType> >
Edit
I've managed to solve it, if my set method looks like:
Base<std::vector<InterfaceType> >::myOption = 10;
It works fine. Still though not sure why I need to specify all template parameters.
myOption is not a dependent name, i.e. it doesn't depend on the template arguments explicitly so the compiler tries to look it up early. You must make it a dependent name:
template <typename InterfaceType>
void ChildClass<InterfaceType>::set()
{
this->myOption = 10;
}
Now it depends on the type of this and thus on the template arguments. Therefore the compiler will bind it at the time of instantiation.
This is called Two-phase name lookup.
C++03 14.6.2 Dependent names
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.
The following code should work.
template <typename InterfaceType>
void ChildClass<InterfaceType>::set()
{
Base<std::vector<InterfaceType> >::myOption = 10;
}