-1

我正在尝试通过 Windows Wave 设备进行扫描,使用以下测试片段test.cpp

using namespace std;
#include <string>
#include <vector>
#include <Windows.h>

int main () 
{ 
    int nDeviceCount = waveOutGetNumDevs();
    vector<wstring> sDevices;
    WAVEOUTCAPS woc;
    for (int n = 0; n < nDeviceCount; n++)
        if (waveOutGetDevCaps(n, &woc, sizeof(WAVEOUTCAPS)) == S_OK) {
            wstring dvc(woc.szPname);
            sDevices.push_back(dvc);
        }
    return 0; 
} 

用 PowerShell 在 PowerShell 中编译gcc version 8.1.0 (i686-posix-dwarf-rev0, Built by MinGW-W64 project),我收到此错误:

PS xxx> g++ .\test.cpp -c
.\test.cpp: In function 'int main()':
.\test.cpp:14:27: error: no matching function for call to 'std::__cxx11::basic_string<wchar_t>::basic_string(CHAR [32])'
    wstring dvc(woc.szPname);

我认为wstring构造函数包括对 c 风格的空终止字符串的支持。为什么我会收到此错误?

4

1 回答 1

2

默认情况下,UNICODE宏未定义。这使得该pzPname字段CHAR pzPname[MAXPNAMELEN]在定义中。这就是出现错误的原因,因为std::wstring它试图用char数据而不是wchar_t数据进行初始化。

要解决此问题,#define UNICODE请在包含文件之前放置一个语句Windows.h,或使用std::string

于 2021-01-02T16:35:51.033 回答