By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 833 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

Templates: Why does this fail to compile?

P: n/a
///
/// Both g++ 3.4.1 and comeau 4.3.3 report "i undefined"
///
#include <iostream>

template<typename T>
class Base
{
protected:
int i;
};
template<typename T>
class Derived : public Base< T >
{
public:
void f() {
i = 0;
std::cout << i << std::endl;
}
};
int main()
{
Derived< int > d;
d.f();
return 0;
}
Jul 23 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
* A Moore:
///
/// Both g++ 3.4.1 and comeau 4.3.3 report "i undefined"
///
#include <iostream>

template<typename T>
class Base
{
protected:
int i;
};
template<typename T>
class Derived : public Base< T >
{
public:
void f() {
i = 0;
std::cout << i << std::endl;
}
};


Here 'i' could conceivably refer to a global variable.

Preferred solution: 'using Base<T>::i;'.

Alternative solution: 'this->i'.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2

P: n/a
A Moore wrote:
#include <iostream>

template<typename T>
class Base
{
protected:
int i;
};
template<typename T>
class Derived : public Base< T >
{
public:
void f() {
i = 0;
In this context non-qualified name 'i' is not a dependent name.
Non-dependent names are looked up immediately in accordance with usual
look-up rules, except that dependent base classes are excluded from this
process. That's why 'i' is not found and the code fails to compile. If
declare an 'i' variable at namespace scope, the code will compile, but
that 'i' will refer to the namespace variable, not to the base class member.

If you want your 'i' to refer to the 'i' in the base class, you have to
explicitly turn it into a dependent name. This can be done by either using

this->i = 0;

or by using a qualified name

Base<T>::i = 0;
std::cout << i << std::endl;
}
};


--
Best regards,
Andrey Tarasevich
Jul 23 '05 #3

P: n/a
Well this->i does not seem very dependent to me...
How come?

Andrey Tarasevich wrote:
A Moore wrote:
#include <iostream>

template<typename T>
class Base
{
protected:
int i;
};
template<typename T>
class Derived : public Base< T >
{
public:
void f() {
i = 0;

In this context non-qualified name 'i' is not a dependent name.
Non-dependent names are looked up immediately in accordance with usual
look-up rules, except that dependent base classes are excluded from this
process. That's why 'i' is not found and the code fails to compile. If
declare an 'i' variable at namespace scope, the code will compile, but
that 'i' will refer to the namespace variable, not to the base class member.

If you want your 'i' to refer to the 'i' in the base class, you have to
explicitly turn it into a dependent name. This can be done by either using

this->i = 0;

or by using a qualified name

Base<T>::i = 0;

std::cout << i << std::endl;
}
};


Jul 23 '05 #4

P: n/a
A Moore wrote:
...
Well this->i does not seem very dependent to me...
How come?
...


'Base<T>' is a dependent type for obvious reasons.

'Derived' is a dependent type in accordance with 14.6.2.1/1 - it is a
compound type constructed from a dependent type ('Base<T>').

'this' is a type-dependent expression in accordance with 14.6.2.2/2 -
class type of the enclosing member function ('Derived') is dependent.

'this->i' is a type-dependent expression in accordance with 14.6.2.2/3 -
its subexpression ('this') is type-dependent.

--
Best regards,
Andrey Tarasevich
Jul 23 '05 #5

P: n/a

"A Moore" <ha******@dontemail.com> wrote in message
news:WS***********@dfw-service2.ext.ray.com...
///
/// Both g++ 3.4.1 and comeau 4.3.3 report "i undefined"
///
#include <iostream>

template<typename T>
class Base
{
protected:
int i;
};
template<typename T>
class Derived : public Base< T >
{
public:
void f() {
i = 0;
std::cout << i << std::endl;
}
};
int main()
{
Derived< int > d;
d.f();
return 0;
}


Try the FAQ:
http://www.parashift.com/c++-faq-lit...html#faq-35.12

(And the next question as well)

Regards,
Sumit.
Jul 23 '05 #6

P: n/a
In article <tk***********@dfw-service2.ext.ray.com>,
A Moore <ha******@dontemail.com> wrote:
Well this->i does not seem very dependent to me...
How come?


Because in the example given it depends upon a Derived<T>
(though the unadorned i may not).
See http://www.comeaucomputing.com/techt...membernotfound
--
Greg Comeau / Comeau for the Mac? Stay tuned.
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Jul 23 '05 #7

P: n/a


All right on vs.net 2003.

Jul 23 '05 #8

P: n/a
Thank you! This was very informative and impressive.

The issue looks very unnatural to me, but then
again I am not well versed in the standard.

Andrey Tarasevich wrote:
A Moore wrote:
...
Well this->i does not seem very dependent to me...
How come?
...

'Base<T>' is a dependent type for obvious reasons.

'Derived' is a dependent type in accordance with 14.6.2.1/1 - it is a
compound type constructed from a dependent type ('Base<T>').

'this' is a type-dependent expression in accordance with 14.6.2.2/2 -
class type of the enclosing member function ('Derived') is dependent.

'this->i' is a type-dependent expression in accordance with 14.6.2.2/3 -
its subexpression ('this') is type-dependent.

Jul 23 '05 #9

P: n/a
Thanks for your help!

Alf P. Steinbach wrote:
* A Moore:
///
/// Both g++ 3.4.1 and comeau 4.3.3 report "i undefined"
///
#include <iostream>

template<typename T>
class Base
{
protected:
int i;
};
template<typename T>
class Derived : public Base< T >
{
public:
void f() {
i = 0;
std::cout << i << std::endl;
}
};

Here 'i' could conceivably refer to a global variable.

Preferred solution: 'using Base<T>::i;'.

Alternative solution: 'this->i'.

Jul 23 '05 #10

P: n/a
Thanks!

Greg Comeau wrote:
In article <tk***********@dfw-service2.ext.ray.com>,
A Moore <ha******@dontemail.com> wrote:
Well this->i does not seem very dependent to me...
How come?

Because in the example given it depends upon a Derived<T>
(though the unadorned i may not).
See http://www.comeaucomputing.com/techt...membernotfound

Jul 23 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.