C++/error message
Expert: Ralph McArdell - 9/2/2008
Question
hello, Ralph
do you know the meaning of the following error message??
error: expected unqualified-id before ‘using’
i get that error when i type in include "filename.h"
before the using namespace std;
i couldnt find any errors in my filename.h file, so i dont
know what that error means
any suggestions would help
thanks
ps: im using g++ compiler in ubuntu
AnswerSorry but without seeing a minimal example that produces the problem I cannot say for sure - human language descriptions are _not_ exact enough.
The error refers to the compiler expecting an unqualified name. This might be because you provided a qualified name when an unqualified name was required or it could be that you have messed up your code and the compiler has got confused as to what is going on. It may even be that the compiler has found a name it does understand - so maybe you are missing additional include files. (note: if filename.h requires other header files included before it then they should most likely be #included in filename.h).
For reference a qualified name is one that specifies the full namespace and type e.g. MyNamespace::MyClass::myStaticMember. An unqualified name is just a name with no :: in it, e.g. myStaticMember.
One area where over-qualification can occur is with the use of enums. In C and C++ the enum type name does not form a name space, unlike namespaces, classes and structs. Thus it should not be used when specifying enumeration values, viz:
enum Colours { black, grey, red, green, blue, white };
// ...
Colours correct = black; // OK
Colours wrong = Colours::black; // BAD. NOT C or C++
One problem with qualifying enumeration values is that the Microsoft Visual C++ compiler has accepted such non-standard qualifications for a long time and as far as I know still accepts them by default (you have to turn off Microsoft language extensions for it not to be accepted). So any code originally written using Microsoft Visual C++ can break for this reason when compiled using more compliant compilers.
Another area that may cause you problems with qualified names has to do with friends and class templates.
From what you say it would seem to be a problem with the filename.h file, even though you think this is not the case. What you think is correct C++ and what the compiler does are probably quite different things - and right or wrong it is the compiler you have to please!
So we have to locate exactly what it is the compiler is complaining about before anything else. First try commenting out the #include line and see if the specific error you mention here error goes away. Of course you will expect to get other errors because filename.h is no longer included, but those are incidental, and should be further into your source file.
If doing this fixes the problem then re-instate the #include "filename.h" line (i.e. uncomment it) and check the end of the filename.h file for possible errors, working your way up the file.
If it does not then you will have to look at other factors - and again I cannot help as you have given no information of other code you have.
If all else fails use a binary chop method of commenting out code in filename.h (or other source files) to home in on the part(s) that have confused the compiler. That is start with approximately the final half of the code commented out. Ensure that you leave enough uncommented at the end to close open sections of code correctly - closing curly braces and the like - if necessary.
If the code compiles with the final half mostly commented out then uncomment the top half of the previously commented out section (i.e. the initial three quarters of the code is now uncommented). If the code does not compile with the latter portion of the code commented out, comment out additionally half of the uncommented code (i.e. so the latter three quarters of the code is now commented out) and re-compile. Repeat, commenting or uncommenting more of the code until you have homed in on the line(s) of code the compiler is complaining about.
Note that you have to be careful to ensure the code is consistent each time you comment or uncomment code, otherwise you will get additional errors introduced.
You can of course use variations on the theme. In general the idea is to reduce what the compiler has to compile until it is obvious exactly what it is that is causing the problem.
For header files you can try just creating a implementation file that only includes that file and compiling this implementation file. Or you can try compiling the header file directly - but you may need to tell the compiler what language to use when doing so, check the g++ documentation (the g++/gcc man page is a good place to start - ensure you have it installed).
Hope this helps you locate the area of code causing the problem if nothing else. As I have said, without seeing a reduced example of the problem code I cannot give you a firm answer. And as soon as you have produced such a short example you will of course have located the problem code yourself and most likely have been able to fix it!