views:

522

answers:

5

Hi, I have a small problem with a define. I want to assign it to an integer variable but the compiler says it's undeclared.

Here's what the code looks like: defines.h

#ifndef DEFINES_H
#define DEFINES_H

#define MYDEFINE 2

#endif

myclass.h

namespace mynamespace {
class myClass {
    int someFunction();
};
}

myclass.cxx

#include "defines.h"
#include "myclass.h"
namespace mynamespace {
int myClass::someFunction() {
    int var = MYDEFINE;
    return 0;
}
}

In the line with the int assignment the compiler error takes place. I also tried to use another define, defined in the same header file as above, as a function parameter with the same effect. Any ideas? Thanks in advance.

I know using defines is kinda bad habit, but I only extend an existing project and I try to stay in their design ways.

EDIT: The error message simply is: Fehler 1 error C2065: 'MYDEFINE': nichtdeklarierter Bezeichner ... As you might see this is not the real source code, but I think I was very careful while putting together the question.

EDIT2: Thanks for the hint with the #warning. There were 2 files with the same name in different folders. I've no idea why the compiler didn't bring this up. Anyway, it works now. Thanks.

A: 

Some other header file also uses DEFINES_H?

One argument for #pragma once...

danio
That tends to make no difference, as you still run into problems with two "defines.h" headers in a project.
MSalters
I would add that #pragma once Microsoft specific, not standard.
Cătălin Pitiș
#pragma once is not just MS, e.g. gcc also implements it. It is not recognized by all preprocessors though so if you're in a really diverse environment it won't be usable.
danio
@MSalters Not so (although other compilers may be broken) - I just tried this with 2 header files with same name in different paths: both get processed, whereas with #ifdef guards they don't.
danio
+2  A: 

You should check whether the symbol MYDEFINE is really defined.

Check whether the header file where it is declared is really included (and compiled). Use #warning near the define to make sure it is compiled for myclass.cxx:

#ifndef DEFINES_H
#define DEFINES_H

#define MYDEFINE 2
#warning My define is defined

#endif

If it is not compiling (you'll not find the warning message in compilation log), make a search for DEFINES_H. It might be already defined somewhere else.

Cătălin Pitiș
+2  A: 

Let's put it all together:

    #ifndef DEFINES_H
    #define DEFINES_H
    #define MYDEFINE 2
    #endif

    namespace mynamespace {
    class myClass {
        int someFunction();
    };    // note ; missing in your code
    }

    namespace mynamespace {
    int myClass::someFunction() {
        int var = MYDEFINE;
        return 0;
    }

This compiles with no errors, so there is something wrong in your #includes.

anon
+1 Nice catch. Little syntax errors usually give the compiler a lot of grief and this is a classic one.
D.Shawley
Thanks for the hint, I forgot the ; and also to #include "myclass.h".
DaClown
+1  A: 

It's probably complaining about you not having declared your class. Try #including "myclass.h"

Edit:

Oh, missing ';' after your class declaration.

veefu
heh heh, oh well. I shouldn't be so literal with example code, i suppose.
veefu
+1 for pointing it out
DaClown
A: 

You need to see what the preprocessor is doing to your code - try compiling myclass.cxx with the -P flag and examining the .i file so generated.

tragomaskhalos