Solution 1 #define MAX 100 #define NEWLINE '\n' #define MIN 0 int main() { // You have defined MAX as 100 above so the following line evaluates to: // const int 100 = 100; which is not valid const int MAX = 100; Also why have you defined NEWLINE ? You should use the ios constant endl to complete a line using cout .
The expected unqualified id error shows up due to mistakes in the syntax. As there can be various situations for syntax errors, you'll need to carefully check your code to correct them. Also, this post points toward some common mistakes that lead to the same error.
A qualified name is one that has some sort of indication of where it belongs, e.g. a class specification, namespace specification, etc. An unqualified name is one that isn't qualified.
The full error is
error: expected unqualified-id before numeric constant
note: in expansion of macro ‘homeid’
string homeid;
^
You're trying to declare a variable with the same name as a macro, but that can't be done. The preprocessor has already stomped over the program, turning that into string 1234;
, which is not a valid declaration. The preprocessor has no knowledge of the program structure, and macros don't follow the language's scope rules.
Where possible, use language features like constants and inline functions rather than macros. In this case, you might use
const int homeid = 1234;
This will be scoped in the global namespace, and can safely be hidden by something with the same name in a narrower scope. Even when hidden, it's always available as ::homeid
.
When you really need a macro, it's wise to follow the convention of using SHOUTY_CAPS
for macros. As well as drawing attention to the potential dangers and wierdnesses associated with macro use, it won't clash with any name using other capitalisation.
That line is fine.
What is most likely happening is that the compiler is complaining not about the macro definition itself, but about the use of the macro. Example:
#define homeid 1234
void homeid() {
}
When compiling this with GCC, I get:
so.cc:1:16: error: expected unqualified-id before numeric constant #define homeid 1234 ^ so.cc:3:6: note: in expansion of macro ‘homeid’ void homeid() { ^
This tells you that the numeric constant prompting the complaint is part of the macro definition, but also that that macro is used (in this case seemingly by accident) on line 3. Take a look at where the macro expansion is coming from in your code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With