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

protected

P: n/a
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;
}

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Jeff Perry" <js*@mail.utexas.edu> wrote...
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
Jul 22 '05 #2

P: n/a
On 8/3/2004 1:28 AM, Jeff Perry wrote:
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

--

Mateusz Łoskot
mateusz at loskot dot net

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #3

P: n/a

"Jeff Perry" <js*@mail.utexas.edu> wrote in message
news:18**************************@posting.google.c om...
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;
}


This issue is now covered by the C++ FAQ:
http://www.parashift.com/c++-faq-lit...html#faq-34.17

Regards,
Sumit.
--
Sumit Rajan <su********@myrealbox.com>

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #4

P: n/a

"Jeff Perry" <js*@mail.utexas.edu> wrote in message
news:18**************************@posting.google.c om...
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-lit....html#faq-34.1
7

-Sharad

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #5

P: n/a
Jeff Perry wrote:
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.)

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.