473,387 Members | 1,453 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.

Templates and Inheritance

1
Hi,
I got a problem I cannot solve and would be most grateful if someone could give me a hint:

under g++ Version 3.2.2 and icc Version 8.0
this code compiles perfectly:

-------------------------------------------------
template<class C>
class A {
public:
C c;
double a;
};

template<class C>
class B : public A<C> {
public:
void init() {
a = 1.0;
};
};
-------------------------------------------------

if I go now to a machine with g++ Version 3.4.x I get
an error message
"error: `a' undeclared (first use this function)"


Thanks so much!
Jul 23 '06 #1
4 3119
Although it is really confusing behaviour between two versions of g++, I know a workaround for this. Just qualifying the member with its class name avoids the compilation error. May be some expert can give an explanation and better solution.

The following will compile without error.

template<class C>
class A {

public:
C c;
double a;
};

template<class C>
class B : public A<C> {
public:
void init() {
A<C>::a = 1.0;
}
};
Jul 26 '06 #2
GazzaH
1
actually that's just a syntax error - you've got
a semicolon after the function definition on the second last line:
};
remove it and wallah!
Aug 17 '06 #3
edam
1
actually that's just a syntax error - you've got
a semicolon after the function definition on the second last line:
};
remove it and wallah!
Not the case! Using GCC 3.4.4 the original code (with extra semi-colon removed) doesn't compile. The error message is:
Expand|Select|Wrap|Line Numbers
  1. test.cc:12: In member function `void B<C>::init()':
  2. test.cc:12: error: `a' undeclared (first use this function)
Nov 20 '06 #4
I noticed the same thing using the mingw compiler (port of gcc) version 3.4.2.
I would also like to find a better alternative than specifying the scope of the variable...
Dec 3 '06 #5

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

Similar topics

1
by: Markus Seeger | last post by:
Hi, I'm writing a commandline argument parser that can handle switches by using some techniques similar to the Qt slot mechanism. example: // inheritance method (what I'm trying at the...
3
by: darkstorm | last post by:
I have a doubt regarding inheritance involving templates Consider this: ///////////////////////////////////// template<typename T> class A { private: T m_a;
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
10
by: makc.the.great | last post by:
now that I am looking at templates, there's the question. why same effect couldn't/shouldn't be achieved with inheritance? provided the difference of the two, when and where do I use templates...
11
by: Peter Oliphant | last post by:
Is there any plan to support templates with managed code in the (near) future? For instance, VS.NET 2005... : )
4
by: qning88 | last post by:
I would like to find out how I can effectively make use of templates in theexample below. In Class A, I have 3 overloaded member functions "send" for handling the different messages. Although...
6
by: Ravi Rao | last post by:
Hi, This is about "templates vs inheritance" (please, before you flame me, I do understand that they cover largely non-intersecting domains). Apart from D&E*, I've found either online...
3
by: Paulo Matos | last post by:
Hi all, I'm curious if I can say in C++ that a certain template can only be instantiated (template-wise) by a class implementing a certain interface (inheriting from a given abstract class). ...
5
by: Lars Hillebrand | last post by:
Hello, i discovered a weird behaviour if i use templates together with virtual inheritance and method over. I managed to reproduce my problem with a small example: // *********** <code...
2
by: Isaac Gelado | last post by:
Hi, I am having problems with inheritance in templates classes. Say I have the following classes: class A {}; class B: public A {}; template<typename Tclass C {}; Now in my code I have...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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,...

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.