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

Base class attribute visibility

P: n/a
Why does the following code not compile on g++-4.0? g++-3.3 does work.

----------
template<typename Scalar, int N>
struct FooData
{
int i;
};

template<typename Scalar, int N>
class Foo
: public FooData<Scalar,N>
{
public:
void test()
{
i = 0;
}
};
----------
$ g++-4.0 -c template.cc -o template.o
template.cc: In member function 'void Foo<Scalar, N>::test()':
template.cc:14: error: 'i' was not declared in this scope
----------

If I add a FooData<Scalar,N>:: in front of the i, it works with g++-4.0. But
i should be visible in Foo without this, right?

Arne

--
[--- PGP key FD05BED7 --- http://www.root42.de/ ---]
Nov 22 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Arne Schmitz wrote:
Why does the following code not compile on g++-4.0? g++-3.3 does work.

----------
template<typename Scalar, int N>
struct FooData
{
int i;
};

template<typename Scalar, int N>
class Foo
: public FooData<Scalar,N>
{
public:
void test()
{
i = 0;
}
};
----------
$ g++-4.0 -c template.cc -o template.o
template.cc: In member function 'void Foo<Scalar, N>::test()':
template.cc:14: error: 'i' was not declared in this scope
----------

If I add a FooData<Scalar,N>:: in front of the i, it works with g++-4.0.
But i should be visible in Foo without this, right?


Try:

this->i = 0;

This is some obscure C++ rule that I never really understood. Maybe someone
else can explain the reasons for that. It has something to do with name
lookup in class templates.

Nov 22 '05 #2

P: n/a
Rolf Magnus wrote:
Arne Schmitz wrote:
Why does the following code not compile on g++-4.0? g++-3.3 does work.

----------
template<typename Scalar, int N>
struct FooData
{
int i;
};

template<typename Scalar, int N>
class Foo
: public FooData<Scalar,N>
{
public:
void test()
{
i = 0;
}
};
----------
$ g++-4.0 -c template.cc -o template.o
template.cc: In member function 'void Foo<Scalar, N>::test()':
template.cc:14: error: 'i' was not declared in this scope
----------

If I add a FooData<Scalar,N>:: in front of the i, it works with g++-4.0.
But i should be visible in Foo without this, right?


Try:

this->i = 0;

This is some obscure C++ rule that I never really understood. Maybe someone
else can explain the reasons for that. It has something to do with name
lookup in class templates.


For the details, see this thread:

http://groups.google.com/group/comp....e537cee49e8dba

Especially the post by Hyman Rosen.

Cheers! --M

Nov 22 '05 #3

P: n/a
In article <3u************@news.dfncis.de>,
Arne Schmitz <ar**********@gmx.net> wrote:
Why does the following code not compile on g++-4.0? g++-3.3 does work.

----------
template<typename Scalar, int N>
struct FooData
{
int i;
};

template<typename Scalar, int N>
class Foo
: public FooData<Scalar,N>
{
public:
void test()
{
i = 0;
}
};
----------
$ g++-4.0 -c template.cc -o template.o
template.cc: In member function 'void Foo<Scalar, N>::test()':
template.cc:14: error: 'i' was not declared in this scope
----------

If I add a FooData<Scalar,N>:: in front of the i, it works with g++-4.0. But
i should be visible in Foo without this, right?


http://www.comeaucomputing.com/techt...membernotfound
--
Greg Comeau / Celebrating 20 years of Comeauity!
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?
Nov 22 '05 #4

P: n/a
Greg Comeau wrote:
If I add a FooData<Scalar,N>:: in front of the i, it works with g++-4.0.
But i should be visible in Foo without this, right?


http://www.comeaucomputing.com/techt...membernotfound


Thanks, I only understand half of it, and cannot imagine an example where it
really makes sense to handle things like this, but at least I know what to
do.

Arne

--
[--- PGP key FD05BED7 --- http://www.root42.de/ ---]
Nov 22 '05 #5

P: n/a
mlimber wrote:
For the details, see this thread:

http://groups.google.com/group/comp....e537cee49e8dba
Especially the post by Hyman Rosen.


Also interesting, but can you give an example, where this lookup rule is
really useful?

Arne

--
[--- PGP key FD05BED7 --- http://www.root42.de/ ---]
Nov 22 '05 #6

P: n/a
In article <3u************@news.dfncis.de>,
Arne Schmitz <ar**********@gmx.net> wrote:
Greg Comeau wrote:
If I add a FooData<Scalar,N>:: in front of the i, it works with g++-4.0.
But i should be visible in Foo without this, right?


http://www.comeaucomputing.com/techt...membernotfound


Thanks, I only understand half of it, and cannot imagine an example where it
really makes sense to handle things like this, but at least I know what to
do.


One way to think of this is that the template is just a description
of an eventual class. What that actual class looks like
we don't know until all the implicit and explicit stuff is done.
Until that, it doesn't know which base members are really there or not.
Note that the extra prods to direct the compiler are not guarantees
(they could be "lies") but there really is no other practical way to
accomplish the intent of melding the extremes involved.
--
Greg Comeau / Celebrating 20 years of Comeauity!
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?
Nov 22 '05 #7

P: n/a
Greg Comeau wrote:
One way to think of this is that the template is just a description
of an eventual class. What that actual class looks like
we don't know until all the implicit and explicit stuff is done.
Until that, it doesn't know which base members are really there or not.
Note that the extra prods to direct the compiler are not guarantees
(they could be "lies") but there really is no other practical way to
accomplish the intent of melding the extremes involved.


Ok, thank you!

Arne

--
[--- PGP key FD05BED7 --- http://www.root42.de/ ---]
Nov 22 '05 #8

P: n/a
Arne Schmitz wrote:
Greg Comeau wrote:

If I add a FooData<Scalar,N>:: in front of the i, it works with g++-4.0.
But i should be visible in Foo without this, right?


http://www.comeaucomputing.com/techt...membernotfound

Thanks, I only understand half of it, and cannot imagine an example where it
really makes sense to handle things like this, but at least I know what to
do.


consider this subcase of your example:

template<typename Scalar, int N>
struct FooData
{
int i;
};

template<>
struct FooData<void*, 0>
{
int haha_i_is_not_declared;
};

template<typename Scalar, int N>
class Foo
: public FooData<Scalar,N>
{
public:
void test()
{
i = 0;
}
};
Nov 22 '05 #9

P: n/a
On 2005-11-17 10:27:14 -0500, Rolf Magnus <ra******@t-online.de> said:
Arne Schmitz wrote:
Why does the following code not compile on g++-4.0? g++-3.3 does work.

----------
template<typename Scalar, int N>
struct FooData
{
int i;
};

template<typename Scalar, int N>
class Foo
: public FooData<Scalar,N>
{
public:
void test()
{
i = 0;
}
};
----------
$ g++-4.0 -c template.cc -o template.o
template.cc: In member function 'void Foo<Scalar, N>::test()':
template.cc:14: error: 'i' was not declared in this scope
----------

If I add a FooData<Scalar,N>:: in front of the i, it works with g++-4.0.
But i should be visible in Foo without this, right?


Try:

this->i = 0;

This is some obscure C++ rule that I never really understood. Maybe someone
else can explain the reasons for that. It has something to do with name
lookup in class templates.


The compiler can't know for sure that you won't add some specialization
of the parent type that either doesn't have a member variable named
"i", or has a type named "i":

template<int N>
class FooData<char, N>
{
//Whoops, there's no member variable 'i'
};

template<int N>
class FooData<unsigned char, N>
{
//Whoops, there a type named 'i'
typedef int i;
};

Now think about what that would mean for the subclass.

--
Clark S. Cox, III
cl*******@gmail.com

Nov 22 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.