接口未被识别

4

我有一个在自己的头文件MyInterface.h中声明的接口:

class MyInterface{
public:
    virtual ~MyInterface(){}
    virtual void initialize() = 0;
    virtual void newValueSound(int stream, double value) = 0;
    virtual void newValueAlg1(int stream, double value) = 0;
    virtual void newValueAlg2(int stream, double value) = 0;
};

每当我包含这个头文件并尝试使用接口时,例如:
#include "MyInterface.h"

void someMethod(){
    MyInterface *interface;
}

我在以下代码行上遇到了奇怪的编译错误:

error C2332: 'struct' : 缺少标签名称
error C2011: '<未命名标记>' : 'enum' 类型重定义
error C2226: 语法错误:意外类型 '<未命名标记>'

我的代码有什么问题?

编辑: 头文件已经有包含保护。 我使用Eclipse CDT与Microsoft编译器。

整个头文件如下:

#ifndef MYINTERFACE_H_
#define MYINTERFACE_H_

class MyInterface{
public:
    virtual ~MyInterface(){}
    virtual void initialize() = 0;
    virtual void newValueSound(int stream, double value) = 0;
    virtual void newValueAlg1(int stream, double value) = 0;
    virtual void newValueAlg2(int stream, double value) = 0;
};

#endif

以及使用它的类:

#ifndef MAIN_H_
#define MAIN_H_

#include <asio.h>
#include "Stream.h"
#include "MyInterface.h"

class MicApp {
private:
    long inputChannelCount;
    Stream **streams;
    MyInterface *interface;
public:
    MicApp(MyInterface *interface);
    void initializeASIODrivers();
    char **getDriverNames(int *numberOfDrivers);
    bool loadDriver(char *driverName);
    ASIOError initDriver(ASIODriverInfo *asioDriverInfo);
    long getChannelCount();
    double getSampleRate();
    void activateStream(bool activate, int stream);
    ASIOError startASIO();
    ASIOError stopASIO();
    ASIOError exitDriver();
};

#endif

请注意,“Stream”是另一个类,可以无问题使用。

1
你的import语句里缺少一个双引号,不过这可能是个打字错误。 - Hunter McMillen
1
你是不是忘记在 #include "MyInterface.h 后面加上引号闭合符号了? - Alexander Pavlov
抱歉,这是一个打字错误。感谢David指出并纠正。在代码中,引号已经正确关闭。 - Jakub Zaverka
这对我来说可以编译通过(gcc 4.6.1)。你用的是什么编译器?你能发一下命令行吗? - David Nehme
1
我终于想通了!问题在于 "interface" 字符串在 ObjBase.h 中被 typedef 了。所以我不能将 "interface" 用作标识符。如果我使用另一个字符串作为标识符,它就可以正常工作。 - Jakub Zaverka
显示剩余7条评论
1个回答

2

问题在于 "interface" 这个字符串在 ObjBase.h 中被 typedef 了。因此我不能使用 "interface" 作为标识符。如果我使用另一个字符串作为标识符,它就可以正常工作。


实际上,它已经被 #define 定义了,因此添加类似 #ifdef interface #undef interface #endif 的内容将解决这个问题。 - DavidJ

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接