Trouble compiling template

A

Artie Gold

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.
No, g++ is right.

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
 
B

Bruce Trask

You cannot initialize a private base member in the derived initialization
list. YOu have to pass the pointer to the Base constructor and let it do
the initialization in its initialization list.

Regards,
Bruce
 
M

Mike Hewson

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);
};

The following is outside the class declaration?:
template<typename T>
TempTest_t<T>::TempTest_t (const char *const pName): m_Name (pName)
{ }

I don't think you have sufficiently associated this last template with
the class ( guessing ).
 
B

Buster

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
{ }
};

That doesn't seem to be a very useful class. m_Name is private so it's
only accessible in member functions or friends of TempTestBase_t. But
there are no friends and the only member functions are the converting
constructor you provided, the copy constructor, the assignment operator
and the destructor - nothing that modifies or examines the value.
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

Yes. Quite aside from that, the TempTestBase_t subobject has to be
constructed, and it has no default constructor so we need to invoke
the right one explicitly.

template<typename T>
TempTest_t<T>::TempTest_t (const char *const pName)
: TempTestBase_t (pName)
{ }
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.

g++ isn't perfect but it was correct to complain.
 
J

Jonathan Mcdougall

Exits Funnel 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.

On what compiler?
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.

I think you want

template <typename T>
TempTest_t<T>::TempTest_t (const char *const pName): TempTestBase_t(pName)
{ }



Jonathan
 
E

Exits Funnel

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.

-exits
 
E

Exits Funnel

Thanks Artie and Bruce. The template stuff fooled me into thinking that
things were more complicated than they were. I feel silly. Thanks again.

-exits
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,197
Messages
2,571,040
Members
47,634
Latest member
RonnyBoelk

Latest Threads

Top