473,387 Members | 1,517 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,387 software developers and data experts.

problem compilng c++ code with a template child class by gcc

Hi! GCC (4.1.2 20070502 (Red Hat 4.1.2-12)) gives an error:

test.cpp: In constructor <Dclass<T>::Dclass()>:
test.cpp:27: error: <a> was not declared in this scope


when compiling the following code:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. //=== declare parent class
  4. template <class T>
  5. class Rclass {
  6.    public:
  7.       T a;         //parent class variable
  8.       Rclass();    //parent class constructor
  9. };
  10.  
  11. //=== parent class constructor
  12. template <class T>
  13. Rclass<T> :: Rclass(){
  14.    a = 1;
  15. };
  16.  
  17. //=== declare child class
  18. template <class T>
  19. class Dclass : public Rclass<T>{
  20.    public:
  21.       Dclass();   //child class constructor
  22. };
  23.  
  24. //=== child class constructor
  25. template <class T>
  26. Dclass<T> :: Dclass(){
  27.     a = 2;  // <===  this parent class variable 'WAS NOT DECLARED IN THIS SCOPE'
  28. };
  29.  
  30. //=== test
  31. int main(){
  32.  
  33.     Dclass<float> b;
  34.     printf("r=%f\n", b.a);
  35.  
  36. return 0;
  37. };
It seems that the compiler doesn't see the variable 'a' in the child class, becasue it was declared in the template parent class. When the type is fixed and templates are not used it runs OK.

However no errors ocur when I try to compile with Borland C++ or older version of GCC.
What can be the problem and the solution? Should I use some options to gcc or declare (use) the variable 'a' in another way ?
Thank you!!
Oct 19 '07 #1
2 2240
weaknessforcats
9,208 Expert Mod 8TB
test.cpp: In constructor <Dclass<T>::Dclass()>:
test.cpp:27: error: <a> was not declared in this scope
The base object has not been created so there is no public data member.

You need to add:
Expand|Select|Wrap|Line Numbers
  1. template <class T>
  2. class Rclass {
  3.    public:
  4.       T a;         //parent class variable
  5.       Rclass();    //parent class constructor
  6.       Rclass(T);                <<<<<
  7. };
  8. template <class T>                              <<<<
  9. Rclass<T> :: Rclass(T arg){                 <<<<
  10.    a = arg;                                           <<<<
  11. };                                                       <<<<
  12. template <class T>
  13. class Dclass : public Rclass<T>{
  14.    public:
  15.       Dclass();   //child class constructor
  16.       Dclass(T);                                     <<<<
  17. };
  18. template <class T>                                             <<<<
  19. Dclass<T> :: Dclass(T arg): Rclass<T>(arg){       <<<<
  20.                                                                          <<<<
  21. };                                                                       <<<<
  22.  
Remember, these are templates. There is no Rclass::a until an Rclass object is created and you create one (and use the Rclass template) by using the initialize list in the Dclass constructor.

Had you not used a template, the you would have had no problem since you would have already declared Rclass to contain a float.

BTW: Don't use public data members.
Oct 19 '07 #2
The base object has not been created so there is no public data member.

Remember, these are templates. There is no Rclass::a until an Rclass object is created and you create one (and use the Rclass template) by using the initialize list in the Dclass constructor.

Had you not used a template, the you would have had no problem since you would have already declared Rclass to contain a float.

BTW: Don't use public data members.

Thank you weaknessforcats!!

Another solution can be:

Expand|Select|Wrap|Line Numbers
  1.  
  2. template <class T>
  3. class Dclass : public Rclass<T>{
  4.    public:
  5.       Dclass();   //child class constructor
  6. };
  7. template <class T>
  8. Dclass<T> :: Dclass(T arg)){ 
  9.  
  10. Rclass<T> :: a = 2;
  11.  
  12. };                                        
  13.  
In this case Rclass object is also created. However your solution seems to be more beautiful. If I have more than one variable in parent class used by child class, I would have to place Rclass<T> :: in front of each of them.
Oct 22 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Ryan M. Keith | last post by:
I am having a problem with the ostream operator in templated classes that I wrote (I'm using the Borland compiler), and I'm certain that the templates are the problem because when I remove the...
5
by: Clifford W. Racz | last post by:
Has anyone solved the issue of translating lists in Word 2003 (WordML) into xHTML? I have been trying to get the nested table code for my XSLT to work for a while now, with no way to get the...
3
by: Thomas Matthews | last post by:
Hi, I would like to apply inheritance to a template parameter, but my design fails to compile: cannot initialize one template class with child child parameterized class. I'll explain... ...
4
by: wkaras | last post by:
I would like to propose the following changes to the C++ Standard, the goal of which are to provide an improved ability to specify the constraints on type parameters to templates. Let me say from...
5
by: nandor.sieben | last post by:
I'm working on a project analyzing a game and I am in need of a tree template library. I found a few implementations on the web but all of them were too complicated for me to understand. I wrote...
0
by: mike | last post by:
Hi there: I've read an excellent "how to"-article by Microsoft (no. 306227) - partly cited cited at the end of this email). I have implemented the code related to the part "How to Add a...
6
by: Hendrik Schober | last post by:
Hi, I have a problem with extending some existing code. In a simplified form, the problem looks like this: I have four types, A, B, C, and D. Each A refers to zero, one, or more B's and each...
2
by: DC | last post by:
Hi, I am trying to implement a simple template control (to be used as TemplateItem etc. in GridView). Does someone see why my code fails? I give up for now and try a different approach (http://...
13
by: Mike -- Email Ignored | last post by:
Is a pure virtual function in allowed in a template base class? In any case, I have one working. Am I skating on thin ice? Thanks, Mike.
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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.