template+inheritance and old code

T

Thomas.Zauner

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

help 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
 
W

wijnand

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
 
T

Thomas.Zauner

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

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
 
M

Markus Moll

Hi

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 said:
TC show_A(void){return pvar[0];};

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

Markus
 
T

tragomaskhalos

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.
 

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

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top