![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Sep 2005
Posts: 2
Rep Power: 0
![]() |
Got compilation error in Fedora Core 4, but it was OK in FC2
I am not sure if this error is caused by faulty C++ programming. Please help.
I created a small project by using KDeveloper in FC2. It worked fine. But when I did the same thing in FC4, unless I change "m_x = 3" to "John<T>::m_x = 3", gcc 4.0.0 always give me a fatal error --- "error: ‘m_x’ was not declared in this scope". The code is trying to instantiate an object from class "Johnson" inherited from a template parent class "John" and call a member function to display a member variable defined in the parent class. The code runs without any problem in VC++6.0. #ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iostream>
#include <cstdlib>
using namespace std;
template <class T>
class John
{
protected:
int m_x;
};
template <class T>
class Johnson:public John <T>
{
public:
void dothis();
};
template <class T>
void Johnson<T>::dothis()
{
m_x = 3;
cout << "x=" << m_x;
}
int main(int argc, char *argv[])
{
cout << "Hello, world!" << endl;
Johnson<int> johnson1;
johnson1.dothis();
return EXIT_SUCCESS;
}Last edited by tw1357; Sep 19th, 2005 at 12:53 AM. |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 884
Rep Power: 4
![]() |
Seems to be a new "feature" of gcc 3.4 and above. See the last item on http://gcc.gnu.org/bugs.html#known. They provide a few different workarounds, but they all need code changes.
It is further explained on http://gcc.gnu.org/onlinedocs/gcc/Na...ml#Name-lookup. Which indicates it is part of the c++ standard that as m_x is used in a context that is not dependent on the type of T, it must be looked up in the enclosing namespace scope (i.e. the global scope). This seems like an odd design decision to me, but I haven't gone through the specs myself. I tried your code in VC7 and it was fine, but gcc 3.4.4 didn't like it. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Sep 2005
Posts: 2
Rep Power: 0
![]() |
Thank you very much, The Dark.
Your answer is exactly what I was looking for. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|