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/ ---] 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.
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
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?
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/ ---]
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?
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/ ---]
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;
}
};
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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. ...
|
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...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
| |