|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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. |
|
![]() |
| Outils de la discussion | |
|
|