C++ – xcode gives syntax error on cpp code

c++, iphone, xcode

I am trying to reuse Apple's Speak Here sample code in my own iPhone app. I downloaded and compiled the project with no problems, so I then added some of the files to my own application. When I try to compile my own application, the compiler gives me

MeterTable.h:54: error: syntax error before 'MeterTable'

The relevant code is:

#include <stdlib.h>#include <stdio.h>#include <math.h>class MeterTable  // line 54{public:

It looks kind of like xcode isn't recognizing MeterTable.h and MeterTable.mm as C++ files. I've checked File>>GetInfo for MeterTable.mm, though, and it lists filetype as sourcecode.cpp.cpp.

Why won't this compile?

Best Solution

  1. You're including "MeterTable.h" in a non C++ file other than MasterTable.mm.
  2. The error is not in 'MeterTable.h' but in the header included before it. Note that <stdlib.h>... can be a noop if they are included before.

If you want to make sure your file is compiled with C++, you can add this code to the begining of MasterTable.mm:

#ifdef __cplusplus#error "compiled as C++"#else#error "compiled as C"#endif