[Insight-users] problems with templates and virtual functions
jean-michel.rouet at philips.com
jean-michel.rouet at philips.com
Fri, 23 Apr 2004 14:21:25 +0200
This is a multipart message in MIME format.
--=_alternative 00446561C1256E7F_=
Content-Type: text/plain; charset="us-ascii"
Hi Samson
On 23/04/2004 07:30:41 Samson Timoner wrote:
>
>class Object {
>virtual void PrintSelf();
>}
>
>template<class T>
>class TemplateClass() : public Object {
>virtual void Update();
>}
>
>class DoSomething() : public TemplateClass<int> {
>void PrintSelf();
>void Update();
>}
>
I tried your small example, and after correcting a few syntactic errors,
it compiled ok, and ran OK (at least with g++)
Here is my corrected code (toto.cxx):
#include <stdio.h>
class Object
{
public:
virtual void PrintSelf() {};
};
template<class T>
class TemplateClass : public Object
{
public:
virtual void Update() {};
};
class DoSomething : public TemplateClass<int> {
public:
void PrintSelf(){
printf ("in PrintSelf\n");
};
void Update(){
printf ("in Update\n");
};
};
int main() {
DoSomething *mydosomething;
mydosomething = new DoSomething();
mydosomething->PrintSelf();
mydosomething->Update();
delete mydosomething;
return 0;
}
At execution I obtain:
rouet at joebar /tmp>g++ toto.cxx -Wall -o toto
rouet at joebar /tmp>./toto
in PrintSelf
in Update
Which is the expected result I guess
Note that if I do not define either PrintSelf() or Update in Object and
TemplateClass as {}, then I obtain a linker error like this:
rouet at joebar /tmp>g++ toto.cxx -Wall -o toto
/tmp/ccCUTt2m.o(.data$_ZTV13TemplateClassIiE+0xc):toto.cxx: undefined
reference
to `TemplateClass<int>::Update()'
collect2: ld returned 1 exit status
--=_alternative 00446561C1256E7F_=
Content-Type: text/html; charset="us-ascii"
<br><font size=2 face="sans-serif">Hi Samson</font>
<br>
<br><font size=2 face="sans-serif">On 23/04/2004 07:30:41 Samson Timoner wrote:</font>
<br><font size=2 face="sans-serif">><br>
>class Object {<br>
>virtual void PrintSelf();<br>
>}<br>
><br>
>template<class T><br>
>class TemplateClass() : public Object {<br>
>virtual void Update();<br>
>}<br>
><br>
>class DoSomething() : public TemplateClass<int> {<br>
>void PrintSelf();<br>
>void Update();<br>
>}<br>
></font>
<br>
<br><font size=2 face="sans-serif">I tried your small example, and after correcting a few syntactic errors, it compiled ok, and ran OK (at least with g++)</font>
<br>
<br><font size=2 face="sans-serif">Here is my corrected code (toto.cxx):</font>
<br><font size=2 face="sans-serif">#include <stdio.h></font>
<br>
<br><font size=2 face="sans-serif">class Object</font>
<br><font size=2 face="sans-serif">{</font>
<br><font size=2 face="sans-serif">public:</font>
<br><font size=2 face="sans-serif"> virtual void PrintSelf() {};</font>
<br><font size=2 face="sans-serif">};</font>
<br>
<br><font size=2 face="sans-serif">template<class T></font>
<br><font size=2 face="sans-serif">class TemplateClass : public Object</font>
<br><font size=2 face="sans-serif">{</font>
<br><font size=2 face="sans-serif">public:</font>
<br><font size=2 face="sans-serif"> virtual void Update() {};</font>
<br><font size=2 face="sans-serif">};</font>
<br>
<br><font size=2 face="sans-serif">class DoSomething : public TemplateClass<int> {</font>
<br><font size=2 face="sans-serif">public:</font>
<br><font size=2 face="sans-serif"> void PrintSelf(){</font>
<br><font size=2 face="sans-serif"> printf ("in PrintSelf\n");</font>
<br><font size=2 face="sans-serif"> };</font>
<br><font size=2 face="sans-serif"> void Update(){</font>
<br><font size=2 face="sans-serif"> printf ("in Update\n");</font>
<br><font size=2 face="sans-serif"> };</font>
<br><font size=2 face="sans-serif">};</font>
<br>
<br><font size=2 face="sans-serif">int main() {</font>
<br><font size=2 face="sans-serif"> DoSomething *mydosomething;</font>
<br>
<br><font size=2 face="sans-serif"> mydosomething = new DoSomething();</font>
<br>
<br><font size=2 face="sans-serif"> mydosomething->PrintSelf();</font>
<br><font size=2 face="sans-serif"> mydosomething->Update();</font>
<br>
<br><font size=2 face="sans-serif"> delete mydosomething;</font>
<br>
<br><font size=2 face="sans-serif"> return 0;</font>
<br><font size=2 face="sans-serif">}</font>
<br>
<br>
<br><font size=2 face="sans-serif">At execution I obtain:</font>
<br><font size=2 face="sans-serif">rouet at joebar /tmp>g++ toto.cxx -Wall -o toto</font>
<br><font size=2 face="sans-serif">rouet at joebar /tmp>./toto</font>
<br><font size=2 face="sans-serif">in PrintSelf</font>
<br><font size=2 face="sans-serif">in Update</font>
<br>
<br><font size=2 face="sans-serif">Which is the expected result I guess</font>
<br>
<br><font size=2 face="sans-serif">Note that if I do not define either PrintSelf() or Update in Object and TemplateClass as {}, then I obtain a linker error like this:</font>
<br><font size=2 face="sans-serif">rouet at joebar /tmp>g++ toto.cxx -Wall -o toto</font>
<br><font size=2 face="sans-serif">/tmp/ccCUTt2m.o(.data$_ZTV13TemplateClassIiE+0xc):toto.cxx: undefined reference</font>
<br><font size=2 face="sans-serif">to `TemplateClass<int>::Update()'</font>
<br><font size=2 face="sans-serif">collect2: ld returned 1 exit status</font>
--=_alternative 00446561C1256E7F_=--