473,394 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Base class attribute visibility

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
9 1870
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Gabriel Genellina | last post by:
Hi In the following code sample, I have: - a Worker class, which could have a lot of methods and attributes. In particular, it has a 'bar' attribute. This class can be modified as needed. - a...
8
by: TravelMan | last post by:
I'm trying to do a show/hide of several elements on a page and can't get it working in Netscape 4.x. All other Windows browsers are working. My elements all have the same class name. <div...
4
by: Colin Desmond | last post by:
Is it possible to write an attribute that changes the visibility of a class (public/private) etc based on some pre-compiler define symbol? We're writing some classes which will be called by an...
10
by: Janis Papanagnou | last post by:
The task seems simple but I am not sure whether this is possible or even the right way to look at it... I have <td class="X"elements where "X" is a placeholder for class names "A", "B", etc. ...
24
by: joeldault | last post by:
Question For Microsoft Access Data Base -------------------------------------------------------------------------------- I am Trying to create a single formula that would do the following: If...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.