LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-27-2005, 06:13 PM   #1
ahwkong
Member
 
Registered: Aug 2004
Location: Australia
Distribution: Fedora
Posts: 282

Rep: Reputation: 30
g++ 3.4 and protected member variable


I have these following files:

t.h
Code:
namespace nc{

    template<typename T>
    class Base {
        T hello;

        protected:
            int test1;
    };

    template<typename T>
    class Next: public Base<T>{

        int test();
    };
};

#include "t.tmpl"
t.tmpl
Code:
template<typename T>
int nc::Next<T>::test(){
    return test1;
}
test.cpp
Code:
#include "t.h"

g++ complaints that, inside Next<T>::test(), test1 is undeclared. However, it is a protected member varibale of base class. It supposed to be visible to the derived class, right?

What's wrong?
 
Old 04-27-2005, 08:20 PM   #2
rununix
LQ Newbie
 
Registered: Aug 2004
Posts: 20

Rep: Reputation: 0
try adding
using namespace nc;

wild guess since you haven't given the rest of code
 
Old 04-27-2005, 11:47 PM   #3
ahwkong
Member
 
Registered: Aug 2004
Location: Australia
Distribution: Fedora
Posts: 282

Original Poster
Rep: Reputation: 30
I have given all the source. It is an example to demonstrate the case. It can be compiled, although the program does not do anything.

Code:
g++ test.cpp
 
Old 04-28-2005, 08:26 AM   #4
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
It compiles fine with g++ 3.3.3 (cygwin special). I don't have 3.4 installed anywhere, so I can't try it with that myself. Something definitely seems odd about that.

BTW, you need the -c flag to compile the example. Otherwise it will try to link in a non-existent main function...

Last edited by deiussum; 04-28-2005 at 08:44 AM.
 
Old 04-28-2005, 09:02 AM   #5
ahwkong
Member
 
Registered: Aug 2004
Location: Australia
Distribution: Fedora
Posts: 282

Original Poster
Rep: Reputation: 30
deiussum,

You are right about the -c flag, but in my case, gcc 3.4.2, the syntax error stops it to go any further to the point of linking, so it does not really matter...

Maybe let me reproduce the error messages here:

Code:
In file included from t.h:18,
                 from test.cpp:1:
t.tmpl: In member function `int nc::Next<T>::test()':
t.tmpl:3: error: `test1' undeclared (first use this function)
t.tmpl:3: error: (Each undeclared identifier is reported only once for each function it appears in.)
 
Old 04-28-2005, 03:50 PM   #6
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Yeah, I didn't mean to say that using the -c flag would take care of your problem, just wanted to clarify that for anyone else trying to compile your example code.

Just for curiosity, what happens when you change the protection level of test1 to public? You are right that it you should be able to access it as protected, but I can't reproduce the error, and don't really have the time to get g++ 3.4.
 
Old 04-28-2005, 04:22 PM   #7
ahwkong
Member
 
Registered: Aug 2004
Location: Australia
Distribution: Fedora
Posts: 282

Original Poster
Rep: Reputation: 30
Sure I understand why you mentioned the flag! That's would help other readers of the thread. :-)

The problem is I am working on a design which requested the field to be protected. That's why I have to ask for an answer :-)
 
Old 04-28-2005, 04:34 PM   #8
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Yep. I understand that you want it to be protected, but I'm just curious if you will still get the error if you switch it to public. If you do, it could be some problem unrelated to its protection level. You SHOULD be able to access it from the derived class as a protected variable, but you can't for some reason. The same code compiles fine for me with 3.3.3 so I can't reproduce your problem to try and figure out why it's occuring.
 
Old 04-28-2005, 04:51 PM   #9
ahwkong
Member
 
Registered: Aug 2004
Location: Australia
Distribution: Fedora
Posts: 282

Original Poster
Rep: Reputation: 30
You have a point. I switched to public and the same error appears... Certianly it is least expected...

I had read the g++ change file before starting this thread but found nothing really related to it. I probably should go back and read again.
 
Old 04-28-2005, 08:07 PM   #10
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
Using g++ 4.0.0, changing the file t.tmpl to:
Code:
template<typename T>
int nc::Next<T>::test(){
    return Base<T>::test1; /* Note: fully qualified name. */
}
makes the program compile and link. I also removed an unnecessary ; after the closing } in the namespace and added include guards (very important for templates). My test program was:
Code:
$ cat main.cpp
Code:
#include "t.hpp" /* I use the .hpp extension for c++-headers, .h for c-headers. */

int main()
{
   nc::Next<int> duh;

   return 0;
}
 
Old 04-29-2005, 08:13 AM   #11
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
It's interesting that 3.4+ makes you fully qualify the name in this case. I wonder how many C++ projects there are that won't compile because of this. I know I seldom fully qualify my member variables unless there is some ambiguity, or I need to call the base class method in a virtual method.
 
Old 04-29-2005, 04:26 PM   #12
ahwkong
Member
 
Registered: Aug 2004
Location: Australia
Distribution: Fedora
Posts: 282

Original Poster
Rep: Reputation: 30
Thanks, Hivemind!

Agree with deiussum... It is going to break a lot of application I believe. Before g++3.4, the principle of template is (the way i understand it) "specify if only there is ambiguity"

Besides, it is very inflexible. If I were to change the base class, I have to do a lot of text substitution. Not a nice solution.

Even use of -fpermissive does not get rid of this as an error. :-(
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ / Passing a variable by reference to a class member function. sepulture Programming 12 11-15-2005 10:23 PM
bash, how to get variable name from variable Duudson Programming 6 01-06-2005 04:38 PM
LQ member progress status from member to senior member............. emailssent LQ Suggestions & Feedback 3 10-11-2004 01:31 PM
calling a variable within a variable sdandeker Programming 9 04-28-2004 03:55 PM
How to get 'Window' variable from a 'Widget' variable bordel Programming 0 11-19-2003 03:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration