0
以下是代碼。C++ 11作爲ReadFileEx回調的lambda
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
int _tmain(int argc, _TCHAR* argv[])
{
auto f = CreateFile(L"file.txt", GENERIC_READ, FILE_SHARE_READ, nullptr,
OPEN_EXISTING, FILE_FLAG_OVERLAPPED, nullptr);
struct overlapped_buffer
{
OVERLAPPED o;
char b[64];
};
overlapped_buffer ob = {};
ReadFileEx(f, ob.b, sizeof(ob.b), &ob.o, [] (DWORD e, DWORD c, OVERLAPPED * o)
{
if(ERROR_SUCCESS == e) printf("Error");
else {
auto ob = reinterpret_cast<overlapped_buffer *>(o);
printf("> %.*s\n", c, ob->b);
}
});
SleepEx(1000, TRUE);
CloseHandle(f);
printf("read file");
return 0;
}
問題是我不知道如何解決intellisense錯誤。
2 IntelliSense: no suitable conversion function from "lambda []void (DWORD e, DWORD c, OVERLAPPED *o)->void" to "LPOVERLAPPED_COMPLETION_ROUTINE" exists c:\kombea\portaudiofastplayer\test_lambda\test_lambda.cpp 19 43 test_lambda
我能否創建一個lambda函數並在此情況下將它用作CALLBACK函數?
什麼版本你正在使用Visual Studio?您的代碼將在VS2010中不起作用(https://connect.microsoft.com/VisualStudio/feedback/details/572138)。 – Praetorian
代碼是否實際編譯?智能感知通常會標記出這樣的東西可以正常工作。 –
否代碼不能編譯。編譯器崩潰。 「錯誤1:錯誤C1001:編譯器中發生內部錯誤。」 – user35025