473,402 Members | 2,064 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,402 software developers and data experts.

Template construction in old gcc 3.3.3 does not compile in gcc 3.4.4

Hello!

Below is a snippet that compiles in 3.3.3, but not in 3.4.4.
Obviously, this is a result of the (laudable) efforts to make gcc more
standards-compliant. Any way to get around this? gcc 3.4.4 complains
that x is undeclared in the template definition.

Thanks,
Andrew Steiner

-----------------------------------------------------------------------

#include <iostream>

using namespace std;

class parent {
public:
parent() { x=3.0; };
double x;
};

template<class parent_t> class child : public parent_t {
public:
void test() {
cout << x << endl;
}
};

int main(void) {
child<parent> c;
c.test();

return 0;
}

Sep 16 '05 #1
5 1208
eknecronzontas wrote:
Below is a snippet that compiles in 3.3.3, but not in 3.4.4.
Obviously, this is a result of the (laudable) efforts to make gcc more
standards-compliant. Any way to get around this? gcc 3.4.4 complains
that x is undeclared in the template definition.

#include <iostream>

using namespace std;

class parent {
public:
parent() { x=3.0; };
double x;
};

template<class parent_t> class child : public parent_t {
public:
void test() {
cout << x << endl;
Change the above line to: cout << this->x << endl;
and all should work as expected.
}
};

int main(void) {
child<parent> c;
c.test();

return 0;
}


Best regards,

Tom

Sep 16 '05 #2

"Thomas Tutone" <Th***********@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
eknecronzontas wrote:
template<class parent_t> class child : public parent_t {
public:
void test() {
cout << x << endl;


Change the above line to: cout << this->x << endl;
and all should work as expected.


parent_t::x works too...

Ali

Sep 16 '05 #3
Hehe...should have thought of that. It seems to me that
parent_t::x should be better, as it doesn't require the
dereferencing of the pointer....

Thanks,
Andrew

Sep 16 '05 #4
eknecronzontas wrote:
template<class parent_t> class child : public parent_t {
public:
void test() {
cout << x << endl; Change the above line to: cout << this->x << endl;
and all should work as expected.
parent_t::x works too...
Hehe...should have thought of that. It seems to me that
parent_t::x should be better, as it doesn't require the
dereferencing of the pointer....


Actually, the two are functionally identical and should result in
identical compiled code. In both cases you're dereferencing the
pointer - the first time explicitly, the second time implicitly, but it
happens either way.

Best regards,

Tom

Sep 16 '05 #5
I ported some gcc 3.3 code over to gcc 4.0 for the first time last week
and had this exact same problem. Copying and pasting the relevant bits
from the gcc 3.4 release page at
http://gcc.gnu.org/gcc-3.4/changes.html:
================================================== ====
In a template definition, unqualified names will no longer find members
of a dependent base (as specified by [temp.dep]/3 in the C++ standard).
For example,

template <typename T> struct B {
int m;
int n;
int f ();
int g ();
};
int n;
int g ();
template <typename T> struct C : B<T> {
void h ()
{
m = 0; // error
f (); // error
n = 0; // ::n is modified
g (); // ::g is called
}
};

You must make the names dependent, e.g. by prefixing them with this->.
Here is the corrected definition of C<T>::h,

template <typename T> void C<T>::h ()
{
this->m = 0;
this->f ();
this->n = 0
this->g ();
}
==============================================

Best,
Nathan

Sep 17 '05 #6

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

Similar topics

21
by: Sebastian Faust | last post by:
Hi, is a construction like the following possible: template<class view_model> class template_clase { protected: template_clase() {} virtual ~template_clase() {}
6
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for...
5
by: Ruben Campos | last post by:
Some questions about this code: template <typename T> class MyTemplate; template <typename T> MyTemplate <T> operator- (const MyTemplate <T> & object); template <typename T> MyTemplate <T>...
5
by: Tony Johansson | last post by:
Hello experts! I have two class template below with names Array and CheckedArray. The class template CheckedArray is derived from the class template Array which is the base class This program...
5
by: Steve | last post by:
Hi, Does C++ allow the programmer to declare a template with in a template so that a generic function can instantiate the embedded template? For example, could code such as this exist: ...
3
by: Martin Vorbrodt | last post by:
In "C++ Templates, The Complete Guide" i read that template copy-con is never default copy constructor, and template assignment-op is never a copy assignment operator. Could someone please explain...
12
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in...
2
by: .rhavin grobert | last post by:
hello;-) i have that following little template that defines some type of vector. it works with structs and i want to use it also for simple pointers. the problem is, in following function......
4
by: suman.nandan | last post by:
Hi C++ Experts ! I have a little weird requirement. I have a base class, say B and lots of classes D1 .. Dn publicly derived from it. Over the course of development the number of derived...
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: 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...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.