-1

我正在开发一个包装 Win32 的mixerXXXAPI 函数的小型 OOP/RAII 库。

我正在编写一个封装MIXERLINE结构的类,所以我的MixerLine类有这个作为它的标题:

#pragma once
#define UNICODE
#include <stdio.h>
#include <string>
#include <windows.h>
#include "MixerDevice.h"

//////////////////////////////

class MixerLine {

private:
    MIXERLINE _mixerLine;

public:

    MixerLine(MixerDevice& parentMixer, DWORD destinationIndex); 

    ~MixerLine();

};

但是我得到一个语法错误(在 VC9 中):

错误 2 错误 C2061:语法错误:标识符 'MixerDevice' d:\tfs\misc\winwavein\mixerline.h

为什么消息没有更多帮助?我不知道它有什么问题。

FWIW,文件中根本没有错误MixerDevice.h

编辑:这是原始的完整文件:

混合线.h

#pragma once

#define UNICODE
#include <stdio.h>
#include <string>

#include <windows.h>

#include "MixerDevice.h"

//////////////////////////////

class MixerLine {

private:
    MIXERLINE _mixerLine;

public:

    MixerLine(MixerDevice& parentMixer, DWORD destinationIndex); 

    ~MixerLine();

    // Properties

    DWORD getDestinationLineIndex() {
        return _mixerLine.dwDestination;
    }   

    DWORD getSourceIndex() {
        return _mixerLine.dwSource;
    }

    DWORD getLineId() {
        return _mixerLine.dwLineID;
    }

    DWORD getStatus() {
        return _mixerLine.fdwLine;
    }

    std:wstring getStatusString() {

        switch( _mixerLine.fdwLine ) {
            case MIXERLINE_LINEF_ACTIVE:
                return L"MIXERLINE_LINEF_ACTIVE";
            case MIXERLINE_LINEF_DISCONNECTED:
                return L"MIXERLINE_LINEF_DISCONNECTED";
            case MIXERLINE_LINEF_SOURCE:
                return L"MIXERLINE_LINEF_SOURCE";
            default:
                return L"";
        }

    }

    DWORD getUserData() {
        return _mixerLine.dwUser;
    }

    DWORD getComponentType() {
        return _mixerLine.dwComponentType;
    }
};

MixerDevice.h

#pragma once
#define UNICODE

#include <memory>
#include <stdio.h>
#include <iostream>
#include <vector>

#include <windows.h>

////////////////////////

#include "MixerLine.h"

class MixerDevice {

    private:
        DWORD     _deviceId;
        HMIXER    _mixerHandle;
        MIXERCAPS _mixerCaps;

    public:
        MixerDevice(DWORD deviceId);
        ~MixerDevice();

        void enumerateLines();

        // Properties

        DWORD getDeviceId() {
            return _deviceId;
        }

        HMIXEROBJ getHandle() {
            return (HMIXEROBJ)_mixerHandle;
        }

        // Caps

        WORD getManufacturerId() {
            return _mixerCaps.wMid;
        }
        WORD getProductId() {
            return _mixerCaps.wPid;
        }
        MMVERSION getDriverVersion() {
            return _mixerCaps.vDriverVersion;
        }
        WCHAR* getProductName() {
            return _mixerCaps.szPname;
        }
        DWORD getSupportBits() {
            return _mixerCaps.fdwSupport;
        }
        DWORD getDestinationCount() {
            return _mixerCaps.cDestinations;
        }
    };

MixerDevice.cpp(我要编译的文件):

#include "MixerDevice.h"

using namespace std;

MixerDevice::MixerDevice(DWORD deviceId) {

    _deviceId = deviceId;

    MMRESULT result;

    result = mixerOpen( &_mixerHandle, deviceId, NULL, NULL, MIXER_OBJECTF_MIXER );
    if( result != MMSYSERR_NOERROR ) throw new exception("Call to mixerOpen failed.", result);

    result = mixerGetDevCaps( (UINT)_mixerHandle, &_mixerCaps, sizeof(MIXERCAPS) );
    if( result != MMSYSERR_NOERROR ) throw new exception("Call to mixerGetDevCaps failed.", result);
}

MixerDevice::~MixerDevice() {

    MMRESULT result = mixerClose( _mixerHandle );
    if( result != MMSYSERR_NOERROR ) exit(666);
}

// Methods

void MixerDevice::enumerateLines() {    
}
4

2 回答 2

2

您的代码中有循环包含,难怪它不起作用。您需要在标题中尽可能用前向声明替换包含。

MixerLine.h - 用前向声明替换包含:

//#include "MixerDevice.h"
class MixerDevice;

并将实现移动到实现文件。

于 2012-06-23T13:19:45.493 回答
2

正如 Luchian Grigore 已经说过的,如果前向声明比包含整个标题(至少,这里和恕我直言)足够的话,它会更好。

#pragma once对循环内含物没有帮助,它只能避免无限内含物。_

但包含开始于MixerDevice.cpp

MixerDevice.cpp -- #include--> MixerDevice.h -- #include--> MixerLine.h -- #include-->MixerDevice.h

由于in ,最后一个#include指令什么也不做。预处理器组装单个文档(翻译单元),看起来像(使用或查看):#pragma onceMixerDevice.h/P/showIncludes

  1. 几个 StdLib 和 windows 标头的内容
  2. MixerLine.h 的内容
  3. MixerDevice.h 的内容
  4. MixerDevice.cpp 的内容

这就是编译器有效地接收到的输入。现在,当编译器到达以下内容中的行时MixerLine.h

MixerLine(MixerDevice& parentMixer, DWORD destinationIndex);

这个名字MixerDevice还不知道;该名称在文档内容的“下方”中介绍MixerDevice.h。您至少需要在MixerDevice使用其名称之前声明该类。您也可以更改您的包含顺序,因为MixerDevice目前不依赖于MixerLine,但是您以后可能会遇到更多麻烦

在与 Luchian Grigore 讨论后,我想指出:

不要通过颠倒包含顺序来解决问题,此解决方案仅适用于这种特殊情况,并且仅当您没有提及MixerDevice.h仅在中声明的名称时才有效MixerLine.h。如 Luchian Grigore 所述,请改用前向声明。

于 2012-06-23T14:08:50.750 回答