7

I have a C++ file like this

#ifndef _MOVE_H
#define _MOVE_H
class Move {
 int x, y;
public:
 Move(int initX = 0, int initY = 0) : x(initX), y(initY) {}
 int getX() { return x; }
 void setX(int newX) { x = newX; }
 int getY() { return y; }
 void setY(int newY) { y = newY; }
};
#endif

And to my amazement, all the code between #ifndef and #endif is simply ignored by the compiler (I swear that I am not defining _MOVE_H anywhere else), and I have all kinds of errors about missing definitions. I was thinking that I did something wrong, but when I try to use another key (like _MOVE_Ha, everything is back to normal. Does _MOVE_H mean something special in C++ ?

I'm running Ubuntu 10.04, GCC 4.4.3, if that matters.

Thanks,

asked Jul 27, 2010 at 15:22
5
  • 1
    may be the one of the library files you are including using that for its inclusion guard? did you check that? btw, I normally prefix it with the project name so as to avoid exactly this type of issue. Commented Jul 27, 2010 at 15:28
  • Another reason to use #pragma once instead of #define include guards... Commented Jul 27, 2010 at 16:02
  • 4
    @Inverse, #pragma once is a compiler extension and not supported by all compilers. Include guards are the only safe compiler independent means of preventing multiple inclusion. Commented Jul 27, 2010 at 16:31
  • @Nathan Ernst: I know, but if you're using any of the 3 major compilers, #pragma once saves the compiler from having to open and process the same guarded header over and over every compile. Commented Jul 28, 2010 at 3:54
  • 1
    There's no need for a compiler to open and process a normal header either. The idea of recognizing include guards was invented and implemented over a decade ago, IIRC. Commented Jul 28, 2010 at 9:21

4 Answers 4

16

just run grep _MOVE_H in /usr/include/c++ on your machine

for me :

c++/4.5.0/bits/move.h:#ifndef _MOVE_H

As a rule of thumb, don't use things (really anything) prefixed by _ or __. It's reserved for internal usage. Use SOMETHING_MOVE_H (usually name of the company, ...).

I guess it's a new header used to add the move semantic to c++0x.

answered Jul 27, 2010 at 15:26
Sign up to request clarification or add additional context in comments.

2 Comments

To clarify the underscore rule, the following things are reserved: any global name prefixed by _, any name of any sort prefixed by __, and any name of any sort prefixed by _ and beginning with a capital letter. The OP runs afoul of the first and third rules.
Any name containing a __ at all is reserved. It need not be prefixed.
6

Anything beginning with an underscore then capital letter is reserved to the implementation. (i.e. _M). I think in general you want to stay away from leading underscores.

answered Jul 27, 2010 at 15:25

Comments

4

I believe gcc has an include file called move.h that includes the sentinel _MOVE_H. Presumably you have collided with this. Use a different identifier, preferably one that doesn't start with an underscore. I put a GUID in mine, but then I'm really obsessive :-)

answered Jul 27, 2010 at 15:29

Comments

0

It's a trick to prevent the same header file being included more than once. The actual value you #define doesn't matter - so long as it's only defined in that header file, the convention is NAME_HEADER_FILE_H in capitals

See also this discussion on #pragma once

answered Jul 27, 2010 at 15:24

1 Comment

sorry, that's not really the problem. the problem is, my file isn't being included at all.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.