protected

J

Jeff Perry

When I try to compile the code below with gcc 3.4.1, I get an error.
In order to get the code to compile, I have to specify i's scope by
prepending 'B::' to 'i' in function D::F(). Can someone tell me why
this is true?

The non-template version of this class hierarchy works without the
scope specifier on 'i'.

template<typename T>
class B {
public:
virtual void F () { }
T i;
};

template<typename T>
class D: public B<T> {
public:
virtual void F () { i = 1; } // error: 'i' undeclared
// but 'B::i = 1;' works
};

int main ()
{
D<unsigned> d;
d.F ();
return 0;
}
 
V

Victor Bazarov

Jeff Perry said:
When I try to compile the code below with gcc 3.4.1, I get an error.
In order to get the code to compile, I have to specify i's scope by
prepending 'B::' to 'i' in function D::F(). Can someone tell me why
this is true?

The non-template version of this class hierarchy works without the
scope specifier on 'i'.

template<typename T>
class B {
public:
virtual void F () { }
T i;
};

template<typename T>
class D: public B<T> {
public:
virtual void F () { i = 1; } // error: 'i' undeclared
// but 'B::i = 1;' works

'i' is what is known as "dependent name". With templates the scope
of the base class is not searched for name resolution unless you
specifically instruct the compiler to do so by either doing B:: or
this->.
};

int main ()
{
D<unsigned> d;
d.F ();
return 0;
}

V
 
?

=?ISO-8859-2?Q?Mateusz_=A3oskot?=

When I try to compile the code below with gcc 3.4.1, I get an error.
In order to get the code to compile, I have to specify i's scope by
prepending 'B::' to 'i' in function D::F(). Can someone tell me why
this is true?

I think it is true. In dynamic polymorphism (virtual)
compiler need in that template qualified name
using :: operator.
template<typename T>
class B {
public:
virtual void F () { }
T i;
};

Why you defined F() that way ?
Shouldn't it be pure virtual function ?

virtual void F () = 0;

Regards
 
S

Sharad Kala

Jeff Perry said:
When I try to compile the code below with gcc 3.4.1, I get an error.
In order to get the code to compile, I have to specify i's scope by
prepending 'B::' to 'i' in function D::F(). Can someone tell me why
this is true?

That's because gcc 3.4.1 implements the two phase name lookup. Changing i to
B<T>::i or this->i makes it a dependent name and delays its lookup. Read
this FAQ -
http://www.parashift.com/c++-faq-lite/containers-and-templates.html#faq-34.1
7

-Sharad
 
B

Ben Hutchings

Jeff said:
When I try to compile the code below with gcc 3.4.1, I get an error.
In order to get the code to compile, I have to specify i's scope by
prepending 'B::' to 'i' in function D::F(). Can someone tell me why
this is true?
<snip>

See question 1 at
<http://www.decadentplace.org.uk/womble/cplusplus/template-faq.html>.

(I've just uploaded that FAQ after working on it occasionally over the
last few months. I'd appreciate comments on anything that should be
added or corrected.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top