[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">&gt;<br>
&gt;class Object {<br>
&gt;virtual void PrintSelf();<br>
&gt;}<br>
&gt;<br>
&gt;template&lt;class T&gt;<br>
&gt;class TemplateClass() : public Object {<br>
&gt;virtual void Update();<br>
&gt;}<br>
&gt;<br>
&gt;class DoSomething() : public TemplateClass&lt;int&gt; {<br>
&gt;void PrintSelf();<br>
&gt;void Update();<br>
&gt;}<br>
&gt;</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 &lt;stdio.h&gt;</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">&nbsp; &nbsp; virtual void PrintSelf() {};</font>
<br><font size=2 face="sans-serif">};</font>
<br>
<br><font size=2 face="sans-serif">template&lt;class T&gt;</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">&nbsp; &nbsp; virtual void Update() {};</font>
<br><font size=2 face="sans-serif">};</font>
<br>
<br><font size=2 face="sans-serif">class DoSomething : public TemplateClass&lt;int&gt; {</font>
<br><font size=2 face="sans-serif">public:</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; void PrintSelf(){</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; printf (&quot;in PrintSelf\n&quot;);</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; };</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; void Update(){</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; printf (&quot;in Update\n&quot;);</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; };</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">&nbsp; &nbsp; DoSomething *mydosomething;</font>
<br>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; mydosomething = new DoSomething();</font>
<br>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; mydosomething-&gt;PrintSelf();</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; mydosomething-&gt;Update();</font>
<br>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; delete mydosomething;</font>
<br>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; 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&gt;g++ toto.cxx -Wall -o toto</font>
<br><font size=2 face="sans-serif">rouet at joebar /tmp&gt;./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&gt;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&lt;int&gt;::Update()'</font>
<br><font size=2 face="sans-serif">collect2: ld returned 1 exit status</font>
--=_alternative 00446561C1256E7F_=--