473,385 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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<inttc(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

Oct 17 '07 #1
4 1387
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

Oct 17 '07 #2

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.comwrote:
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

Oct 17 '07 #3
Hi

Th***********@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

Oct 17 '07 #4
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.
Oct 17 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Steve | last post by:
Hi all, Is template code "always" inlined??? Or will compilers instantiate template functions separately if they are large? Also, what happens to the code if explicit instantiation mechanism...
3
by: W. Cerven | last post by:
I am using the C++ STL container "list" to create a template nested list. Attached are my basic code and compiler errors. Oddly enough, I when I create a non-template version of this nested list,...
3
by: Gandu | last post by:
Could some C++ guru please help me. I have a template based general linked list class that I want to inherit publicly to create a queue class. In the case of non-template inheritance, I can use:...
3
by: Matteo Settenvini | last post by:
Mmmh... I was trying some code, and I bumped in a strange behaviour. The following piece of code compiles normally : //////// CODE STARTS ///////////////// #include <iostream> class B {...
4
by: zl2k | last post by:
hi, all Suppose I have a template class A template<class Tclass A{...} Then I have another class B which inherits class A. Should I say class B: public A{...} or class B: template<class...
3
by: little.freaky | last post by:
Hello group, I have a problem with template classes and inheritance. I've searched on the internet to find a solution but all the examples look the same as my code (as far as I can tell) and I...
0
by: Jack | last post by:
Hi, I want to override an overridable method in some base class from my class. It works fine if I manually code the method (as one would expect), but I want the IDE to auto-generate the template...
2
by: aitrob | last post by:
Hi, I have a problem concerning templates/inheritance. I have a code that compiles fine with g++ 4.0.1 (Apple version), but gives a lot of errors with Intel C++ 10.1 (Mac OS X). I'm not sure if...
0
by: Thelma Roslyn Lubkin | last post by:
dizzy <dizzy@roedu.netwrote: : Hello : When working with template heavy code one will have to face the : inherent "problems" of the inclusion model because of the requirement : to provide the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.