Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 19th, 2005, 12:29 AM   #1
tw1357
Newbie
 
Join Date: Sep 2005
Posts: 2
Rep Power: 0 tw1357 is on a distinguished road
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.
tw1357 is offline   Reply With Quote
Old Sep 19th, 2005, 4:23 AM   #2
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 884
Rep Power: 4 The Dark is on a distinguished road
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.
The Dark is offline   Reply With Quote
Old Sep 19th, 2005, 11:06 PM   #3
tw1357
Newbie
 
Join Date: Sep 2005
Posts: 2
Rep Power: 0 tw1357 is on a distinguished road
Thank you very much, The Dark. Your answer is exactly what I was looking for.
tw1357 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:22 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC