A
Artie Gold
No, g++ is right.Exits said:Hello,
I've been tasked with porting some C++ code from Windows to Linux. The
following excerpt is giving me trouble:
//BEGIN CODE
#include <string>
class TempTestBase_t
{
std::string m_Name; // what's my name?
public:
TempTestBase_t(const char *const pName) : m_Name (pName) // copy
name for self
{ }
};
template<typename T>
class TempTest_t
: public TempTestBase_t
{
public:
TempTest_t (const char *const pName);
};
template<typename T>
TempTest_t<T>::TempTest_t (const char *const pName): m_Name (pName)
{ }
//END EXCERPT
When I issue 'g++ -c -o temptest temptest.cpp', g++ (version Red Hat
Linux 3.2.3 - 42) complains thusly:
temptest.cpp: In constructor `TempTest_t<T>::TempTest_t(const char*)':
temptest.cpp:5: `std::string TempTestBase_t::m_Name' is private
temptest.cpp:29: within this context
NOTE: The line numbers in the errors don't correspond exactly with the
above code because after cutting pasting there were issues with the
newlines.
Presumably, this code compiles on windows. So, is g++ broken or is
there really a problem with the code? It looks okay to me but I'm no
template expert. Thanks in advance for any thoughts.
Consider the offending constructor. In it you initialize the value of
`m_Name', which is a *private* member of the class from which you
inherit. As a pair of Liverpudlians wrote forty years ago, "You Can't Do
That".
Change the constructor to:
template<typename T>
TempTest_t<T>::TempTest_t (const char *const pName)
: TempTestBase_t (pName) // initialize the superclass
{ }
and all will be well.
Oh, and by the way, the fact that it's a template is orthogonal to your
problem.
HTH,
--ag