Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Template construction in old gcc 3.3.3 does not compile in gcc 3.4.4

Reply
Thread Tools

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

 
 
eknecronzontas@yahoo.com
Guest
Posts: n/a
 
      09-16-2005
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;
}

 
Reply With Quote
 
 
 
 
Thomas Tutone
Guest
Posts: n/a
 
      09-16-2005
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

 
Reply With Quote
 
 
 
 
=?iso-8859-1?Q?Ali_=C7ehreli?=
Guest
Posts: n/a
 
      09-16-2005

"Thomas Tutone" <> wrote in message
news: 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: works too...

Ali

 
Reply With Quote
 
eknecronzontas@yahoo.com
Guest
Posts: n/a
 
      09-16-2005
Hehe...should have thought of that. It seems to me that
parent_t: should be better, as it doesn't require the
dereferencing of the pointer....

Thanks,
Andrew

 
Reply With Quote
 
Thomas Tutone
Guest
Posts: n/a
 
      09-16-2005
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: works too...


> Hehe...should have thought of that. It seems to me that
> parent_t: 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

 
Reply With Quote
 
Nathan Addy
Guest
Posts: n/a
 
      09-17-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to compile the following source code in VC6// I have error inVC++6 but compile ok in GCC fAnSKyer C++ 2 06-07-2009 07:57 AM
cant compile on linux system.cant compile on cant compile onlinux system. Nagaraj C++ 1 03-01-2007 11:18 AM
private construction on GCC brianhray@gmail.com C++ 9 07-28-2006 02:24 PM
Default construction versus construction with initial values Ook C++ 10 10-08-2005 09:00 PM
va_start and template functions do not compile under gcc 2.7.2 frankg C++ 4 07-28-2003 12:12 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57