xcode给出了cpp代码的语法错误

时间:2021-10-26 15:37:40

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

我正在尝试在我自己的iPhone应用程序中重用Apple的Speak Here示例代码。我没有任何问题地下载并编译了项目,因此我将一些文件添加到我自己的应用程序中。当我尝试编译自己的应用程序时,编译器给了我

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

MeterTable.h:54:错误:'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.

看起来有点像xcode没有将MeterTable.h和MeterTable.mm识别为C ++文件。我已经检查了文件>> GetInfo for MeterTable.mm,它将文件类型列为sourcecode.cpp.cpp。

Why won't this compile?

为什么不编译?

3 个解决方案

#1


  1. You're including "MeterTable.h" in a non C++ file other than MasterTable.mm.
  2. 您在除MasterTable.mm之外的非C ++文件中包含“MeterTable.h”。

  3. 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.
  4. 错误不在'MeterTable.h'中,而是在它之前包含的标题中。请注意,如果之前包含 ...可以是noop。

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

如果要确保使用C ++编译文件,可以将此代码添加到MasterTable.mm的开头:

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

#2


DO the following things and then try:

做以下事情,然后尝试:

  1. Go to the Build Setting and search "Compile Sources As"
  2. 转到构建设置并搜索“将源编译为”

  3. Select "According to file type"
  4. 选择“根据文件类型”

  5. If you are including CPP file in any of your Objective C class then its implementation class should be .mm, just change its extension to .mm from.m.
  6. 如果您在任何Objective C类中包含CPP文件,那么它的实现类应该是.mm,只需将其扩展名从.m更改为.mm。

  7. Remember, even if you are using CPP code hierarchically, you need to change the extension to .mm.
  8. 请记住,即使您按层次使用CPP代码,也需要将扩展​​名更改为.mm。

#3


Try to enclose #include directives with extern "C" { }

尝试用extern“C”{}括起#include指令

#1


  1. You're including "MeterTable.h" in a non C++ file other than MasterTable.mm.
  2. 您在除MasterTable.mm之外的非C ++文件中包含“MeterTable.h”。

  3. 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.
  4. 错误不在'MeterTable.h'中,而是在它之前包含的标题中。请注意,如果之前包含 ...可以是noop。

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

如果要确保使用C ++编译文件,可以将此代码添加到MasterTable.mm的开头:

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

#2


DO the following things and then try:

做以下事情,然后尝试:

  1. Go to the Build Setting and search "Compile Sources As"
  2. 转到构建设置并搜索“将源编译为”

  3. Select "According to file type"
  4. 选择“根据文件类型”

  5. If you are including CPP file in any of your Objective C class then its implementation class should be .mm, just change its extension to .mm from.m.
  6. 如果您在任何Objective C类中包含CPP文件,那么它的实现类应该是.mm,只需将其扩展名从.m更改为.mm。

  7. Remember, even if you are using CPP code hierarchically, you need to change the extension to .mm.
  8. 请记住,即使您按层次使用CPP代码,也需要将扩展​​名更改为.mm。

#3


Try to enclose #include directives with extern "C" { }

尝试用extern“C”{}括起#include指令