PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > template+inheritance and old code
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
template+inheritance and old code

Réponse
 
LinkBack Outils de la discussion
Vieux 17/10/2007, 12h17   #1
Thomas.Zauner@icp.uni-stuttgart.de
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut template+inheritance and old code


Hi,

i have to get an aprx. 9 year old code running. back then id did
compile but now it doesn't.
i have broken down the problem to the following technique which is
used quit freq. in the code.
don't ask me why it was used or if it makes sense, i don't even know
the guy who wrote it personally.

If you have any information about:

a) why this is not working anymore.
b) when did this stop working
c) can i use a compiler switch to accept the code (will it run?)


BTW: i work under linux using gcc and/or the intel c++ compiler

would be so much appreciated .-)

--------------------------------------------------------------------------------------------
#include <iostream>

using namespace std;

template<class T,int dim>
class TBase
{
public:
TBase(T val){pvar[dim-1]=val;};
T show_B(void){ return pvar[dim-1];};
protected:
T pvar[dim];
};

template <class TC>
class TClass: public TBase<TC,1>
{
public:
TClass(TC bval):TBase<TC,1>(bval){ pvar[0]=bval; };
TC show_A(void){return pvar[0];};
};

int main (void)
{
TClass<int> tc(10);
cout << tc.show_B() << tc.show_A() << endl;
return 0;
}

---------------------------------------------------------------------------------------


error (here in g++):
-----------------------------------------------------------------------------------------
g++ -W -Wall -o runtest maintest.cc
maintest.cc: In constructor 'TClass<TC>::TClass(TC)':
maintest.cc:20: error: 'pvar' was not declared in this scope
maintest.cc: In member function 'TC TClass<TC>::show_A()':
maintest.cc:21: error: 'pvar' was not declared in this scope

  Réponse avec citation
Vieux 17/10/2007, 12h52   #2
wijnand
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: template+inheritance and old code

Hi,

replace every reference to 'pvar' in TClass with 'this->pvar', and it
works (at least with my g++)

> template <class TC>
> class TClass: public TBase<TC,1>
> {
> public:
> TClass(TC bval):TBase<TC,1>(bval){ pvar[0]=bval; };
> TC show_A(void){return pvar[0];};
>
> };


becomes:

template <class TC>
class TClass: public TBase<TC,1>
{
public:
TClass(TC bval):TBase<TC,1>(bval){ this->pvar[0]=bval; };
TC show_A(void){return this->pvar[0];};

};


Wijnand

  Réponse avec citation
Vieux 17/10/2007, 12h59   #3
Thomas.Zauner@icp.uni-stuttgart.de
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: template+inheritance and old code


Hi,

Thx Wijnand. i just noticed that in 1999 the namespace issue was
diffrent from now. and someone probably
added some "use namespace std;" later and this is why all the vars
were not found.

Thomas

On Oct 17, 1:52 pm, wijnand <wijnandsuij...@gmail.com> wrote:
> Hi,
>
> replace every reference to 'pvar' in TClass with 'this->pvar', and it
> works (at least with my g++)
>
> > template <class TC>
> > class TClass: public TBase<TC,1>
> > {
> > public:
> > TClass(TC bval):TBase<TC,1>(bval){ pvar[0]=bval; };
> > TC show_A(void){return pvar[0];};

>
> > };

>
> becomes:
>
> template <class TC>
> class TClass: public TBase<TC,1>
> {
> public:
> TClass(TC bval):TBase<TC,1>(bval){ this->pvar[0]=bval; };
> TC show_A(void){return this->pvar[0];};
>
> };
>
> Wijnand



  Réponse avec citation
Vieux 17/10/2007, 13h02   #4
Markus Moll
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: template+inheritance and old code

Hi

Thomas.Zauner@icp.uni-stuttgart.de wrote:

> i have to get an aprx. 9 year old code running. back then id did
> compile but now it doesn't.


9 year old? I've seen younger code that looked a lot worse.

> i have broken down the problem to the following technique which is
> used quit freq. in the code.
> don't ask me why it was used or if it makes sense, i don't even know
> the guy who wrote it personally.
>
> If you have any information about:
>
> a) why this is not working anymore.


Because of dependent name lookup. At least GCC used to do name lookup wrong
for dependent names, which made the code compile.

> b) when did this stop working


Depends on the compiler.

> c) can i use a compiler switch to accept the code (will it run?)


It might be possible, but it would be better to fix the code (which is
luckily rather easy)


> #include <iostream>
>
> using namespace std;
>
> template<class T,int dim>
> class TBase
> {
> public:
> TBase(T val){pvar[dim-1]=val;};
> T show_B(void){ return pvar[dim-1];};
> protected:
> T pvar[dim];
> };
>
> template <class TC>
> class TClass: public TBase<TC,1>
> {
> public:
> TClass(TC bval):TBase<TC,1>(bval){ pvar[0]=bval; };


The problem is that pvar is not a dependent name (it does not depend on any
template parameters), so it is looked up when the template definition is
processed. At that point, it is uncertain what template base classes look
like, so they are not included in lookup. The easy fix is to make the name
dependent by writing e.g. this->pvar instead:

TClass(TC bval) : TBase<TC,1>(bval) { this->pvar[0] = bval; }

> TC show_A(void){return pvar[0];};


The same here:
TC show_A() { return this->pvar[0]; }

Markus

  Réponse avec citation
Vieux 17/10/2007, 13h14   #5
tragomaskhalos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: template+inheritance and old code

On Oct 17, 12:59 pm, Thomas.Zau...@icp.uni-stuttgart.de wrote:
> Hi,
>
> Thx Wijnand. i just noticed that in 1999 the namespace issue was
> diffrent from now. and someone probably
> added some "use namespace std;" later and this is why all the vars
> were not found.
>


I don't think this has got anything to do with it -
see Markus's reply re: the issue of dependent names.


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h14.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13422 seconds with 13 queries