g++ 3.4 and protected member variable
I have these following files:
t.h Code:
namespace nc{ Code:
template<typename T> 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? |
try adding
using namespace nc; wild guess since you haven't given the rest of code |
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 |
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... |
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, |
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. :) |
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 :-) |
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.
|
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. |
Using g++ 4.0.0, changing the file t.tmpl to:
Code:
template<typename T> Code:
$ cat main.cpp Code:
#include "t.hpp" /* I use the .hpp extension for c++-headers, .h for c-headers. */ |
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.
|
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. :-( |
All times are GMT -5. The time now is 08:39 PM. |