【问题标题】:How to fix ''Expected ';' after top level declarator" and "Unknown type name 'class'?如何修复''预期';'在顶级声明符之后”和“未知类型名称‘类’?
【发布时间】:2019-05-29 08:19:51
【问题描述】:

我使用 cocoa 将腾讯用 Objective-c 编写的 imSDK 演示 TUIKit 导入到我的空白 swift 应用程序中。我得到错误:预期的';'在顶级声明符和未知类型名称“类”之后;你是说“类”吗?

出现错误的文件是 wav.h 并且有 wav.mm 文件。代码如下:

文件 wav.h

#ifndef WAV_H
#define WAV_H
#include <stdio.h>
class WavWriter {
public:
    WavWriter(const char *filename, int sampleRate, int bitsPerSample, int channels);
    ~WavWriter();
    void writeData(const unsigned char* data, int length);
private:
    void writeString(const char *str);
    void writeInt32(int value);
    void writeInt16(int value);
    void writeHeader(int length);
    FILE *wav;
    int dataLength;
    int sampleRate;
    int bitsPerSample;
    int channels;
};
#endif

错误在“class WavWriter”这一行。

wav.mm 文件

#import <UIKit/UIKit.h>
#include "wav.h"
void WavWriter::writeString(const char *str) {

    fputc(str[0], wav);
    fputc(str[1], wav);
    fputc(str[2], wav);
    fputc(str[3], wav);
}

....

我搜索了两天,找不到答案。有什么帮助吗?谢谢。

【问题讨论】:

  • 您是否在项目中添加了桥接头?
  • 感谢您的帮助,Johnykutty,祝您有美好的一天!

标签: objective-c xcode objective-c++ swift4.2


【解决方案1】:

您的wav.h 文件管理器包含在不是.mm 文件的编译单元中(这意味着“编译为Objective-C++”,即.m.c 文件。

你需要用

括起来类定义
#if defined __cplusplus

// the C++ defs

#endif

#ifdef ... #endif 之间的行将被忽略,除非正在编译的文件是 C++ 或 Objective-C++。

如果你有需要知道类型存在的直接 Objective-C 文件,你可以使用 typedefvoid* 或使其成为不完整的 struct 类型

#if defined __cplusplus

// the C++ defs

class WavWriter {
// ...
};

#else

typedef struct WavWriter WavWriter; // Sets up incomplete type for C code

#endif

@interface Foo

@private
    WavWriter* writer; // In C++ a pointer to your class, in C a pointer to an opaque type.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-12
    • 1970-01-01
    • 2016-12-28
    • 2018-11-02
    • 1970-01-01
    • 2014-11-18
    • 1970-01-01
    相关资源
    最近更新 更多