0

I'm getting this error in a header file:

error: expected initializer before ‘<’ token

class MyEntity;
typedef std::map<uint16,MyEntity*> myList_t;

I figured it's not seeing the map include, but at the top of that header file is:

#include <list>
#include <map>

In another header file:

typedef unsigned int               uint32;

Any ideas?

g++ (GCC) 4.1.2 20080704 (Red Hat 4.1.2-48)

9
  • 1
    Are you sure this is the right line? Can you post a complete example that reproduces the error? Commented Jul 2, 2011 at 18:04
  • could you please tell us, which compiler you are using, and on which platform? Thx Commented Jul 2, 2011 at 18:04
  • 1
    This code compiles for me, if I put the line "typedef short uint16;" in front of it. Where is your uint16 type defined at? Commented Jul 2, 2011 at 18:05
  • 2
    Are you sure uint16 is a type? The standard types are called uint16_t etc.
    – Kerrek SB
    Commented Jul 2, 2011 at 18:11
  • 4
    Try to provide a minimal complete example that still has this error. Chances are you'll find the bug yourself and if not, we'll be in a better position to help.
    – hammar
    Commented Jul 2, 2011 at 18:23

2 Answers 2

1

There was a macro named map that was causing a conflict.

3
  • 1
    This is why macros need to be ALL UPPERCASE.
    – moshbear
    Commented Dec 11, 2011 at 4:06
  • It was. #define _MAP_H Once I changed that to BMAP and the map files too, it was fixed.
    – Zeno
    Commented Dec 11, 2011 at 5:53
  • You should use MAP_H instead of _MAP_H. An identifier starting with an underscore followed by another underscore or a capital letter is reserved for the C compiler and/or library.
    – moshbear
    Commented Dec 11, 2011 at 7:30
-1

In your actual code (not your simplified example) you are probably missing a ; from the end of the line before the typedef. This is usually what it means when I get that kind of cryptic error and I can almost reproduce your message by putting another underminated typedef above it:

q.cc:8: error: expected initializer before 'typedef'

My first guess was that class MyEntity was a full definition (not a forward declaration) and was missing the ; but I get a slightly different error for that.

4
  • That almost, but not quite the same error-message, and the difference is significant here. Commented Jul 2, 2011 at 18:35
  • The error message will depend on what's on the line above, which I suspect is not reproduced in the question. Commented Jul 2, 2011 at 18:36
  • In this case, if the line above has a semicolon missing or something of the likes, the error will always be caused by the keyword typedef. The compiler will get get to the <, as indicated by the error-message of the OP. Commented Jul 2, 2011 at 18:39
  • Every map use in this file gets the same error, the rest of the code doesn't have any weird errors.
    – Zeno
    Commented Jul 2, 2011 at 19:42

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.