Connecting Tech Pros Worldwide Forums | Help | Site Map

Trouble compiling template

Exits Funnel
Guest
 
Posts: n/a
#1: Jul 23 '05
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


Artie Gold
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Trouble compiling template


Exits Funnel wrote:[color=blue]
> 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.
>[/color]
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


--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Bruce Trask
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Trouble compiling template


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
"Exits Funnel" <exitsfunnel@noyahoospam.com> wrote in message
news:c5ICd.8409$512.2467@fe12.lga...[color=blue]
> 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
>[/color]


Mike Hewson
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Trouble compiling template


Exits Funnel wrote:[color=blue]
> 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);
> };
>[/color]

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

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

--

Cheers
--
Hewson::Mike
"This letter is longer than usual because I lack the time to make it
shorter" - Blaise Pascal
Exits Funnel
Guest
 
Posts: n/a
#5: Jul 23 '05

re: Trouble compiling template


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

Bruce Trask wrote:
[color=blue]
> 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
> "Exits Funnel" <exitsfunnel@noyahoospam.com> wrote in message
> news:c5ICd.8409$512.2467@fe12.lga...
>[color=green]
>>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
>>[/color]
>
>
>[/color]

Mike Hewson
Guest
 
Posts: n/a
#6: Jul 23 '05

re: Trouble compiling template


Mike Hewson wrote:

<snip>

Ignore that, I'm wrong. :-(

--

Cheers
--
Hewson::Mike
"This letter is longer than usual because I lack the time to make it
shorter" - Blaise Pascal
Buster
Guest
 
Posts: n/a
#7: Jul 23 '05

re: Trouble compiling template


Exits Funnel wrote:[color=blue]
> 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
> { }
> };[/color]

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.
[color=blue]
> 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[/color]

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)
{ }
[color=blue]
> 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.[/color]

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

--
Regards
Buster
Jonathan Mcdougall
Guest
 
Posts: n/a
#8: Jul 23 '05

re: Trouble compiling template



"Exits Funnel" <exitsfunnel@noyahoospam.com> a écrit dans le message de
news: c5ICd.8409$512.2467@fe12.lga...[color=blue]
> 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.[/color]

On what compiler?
[color=blue]
> 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.[/color]

I think you want

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



Jonathan


Exits Funnel
Guest
 
Posts: n/a
#9: Jul 23 '05

re: Trouble compiling template


[color=blue][color=green]
>>
>>Presumably, this code compiles on windows.[/color]
>
>
> On what compiler?[/color]

VC++ 7.1. Go figure.

-exits

Closed Thread