Sunday, February 15, 2009

[Cpp-Programming] Template problem, data member undefined!!!

Please take a look at the following code,
my question is: why I have to use
Base<_Tp>::datamember to refer the datamember
in the base class in the derived class?
When I use datamember in the derived class,
the compilation would fail.

I use gcc 3.4.4 (cygwin)

#include <iostream>

template <typename _Tp>
class Base
{
protected:
_Tp numArray[3];
Base()
{
numArray[0] = static_cast<_Tp>(1);
numArray[1] = static_cast<_Tp>(11);
numArray[2] = static_cast<_Tp>(111);
};
};

template <typename _Tp>
class Deriv: public Base<_Tp> {
public:
Deriv() {

/* Why the following commented code can not be successfully compiled?
* I got the numArray undefined error !!
*
std::cout << numArray[0] << std::endl;
std::cout << numArray[1] << std::endl;
std::cout << numArray[2] << std::endl;
*/

/* correct code */
std::cout << Base<_Tp>::numArray[0] << std::endl;
std::cout << Base<_Tp>::numArray[1] << std::endl;
std::cout << Base<_Tp>::numArray[2] << std::endl;
};
};

int main(void)
{
Deriv<int> de;

return 0;
}


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "C++ Programming" group.
To post to this group, send email to Cpp-Programming@googlegroups.com
To unsubscribe from this group, send email to Cpp-Programming+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/Cpp-Programming?hl=en
-~----------~----~----~----~------~----~------~--~---

0 Comments:

Post a Comment

<< Home