I do not understand - what is the error?

Code:

const char MFName_r[] = "1223_r"; // Название файла для File Mapping для чтения const char MFName_w[] = "1223_w"; // Название файла для File Mapping для чтения // Структуры struct FMP_struct{ HANDLE readfile; HANDLE writefile; int* read; int* write; } FMP; ///////////////////////////////////////////////////////////////////// // // // Работа с File Mapping // // ///////////////////////////////////////////////////////////////////// // Создание Map файла void LetMeMapFlie(){ // Создаем файлы FMP.writefile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 255, MFName_w); FMP.readfile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READONLY, 0, 255, MFName_r); // Открываем файлы FMP.write = (int*)MapViewOfFile(FMP.writefile, FILE_MAP_WRITE, 0,0,0); FMP.read = (int*)MapViewOfFile(FMP.readfile, FILE_MAP_READ, 0,0,0); } // Считать CHAR из MAP unsigned char ReadChar_SlimRG(){ return (char)FMP.read[0]; } // Считать INT из MAP int ReadInt_SlimRG(){ return (int)FMP.read[0]; } // Записать INT в MAP void WriteInt_SlimRG(int key){ FMP.writefile[0] = (int)key; } // Записать CHAR в MAP void WriteChar_SlimRG(unsigned char key){ FMP.writefile[0]) = (int)key; } 

Writes - subscript of pointer to incomplete type 'void'

And highlights:

 void WriteInt_SlimRG(int key){ FMP.writefile[0] = key; } 

and

 void WriteChar_SlimRG(unsigned char key){ FMP.writefile[0]) = (int)key; } 

Closed due to the fact that off-topic participants VTT , Fat-Zer , LFC , freim , aleksandr barakin 4 Feb at 14:11 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - VTT, Fat-Zer, LFC, freim
If the question can be reformulated according to the rules set out in the certificate , edit it .

    2 answers 2

    Because the memory access should go to the address FMP.write , and not to the handle FMP.writefile

    • Yes, but, MapOfViewFile gives the same LPVOID result will be the same, TS'u needs an explicit type conversion. - LLENN
    • @LLENN It is done by FMP.write = (int*)MapViewOfFile - MBo
    • Just not paying attention :) - LLENN
    • Thank you - doper - SlimRG

    Try doing this and note the explicit type conversions:

     #include <Windows.h> #include <iostream> typedef struct memory_mapped_file { explicit memory_mapped_file(const size_t file_size) : read_handle(nullptr), write_handle(nullptr), read_ptr(nullptr), write_ptr(nullptr) { write_handle = CreateFileMapping(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, 0, file_size, L"mm_file_rw"); read_handle = CreateFileMapping(INVALID_HANDLE_VALUE, nullptr, PAGE_READONLY, 0, file_size, L"mm_file_ro"); read_ptr = MapViewOfFile(read_handle, FILE_MAP_READ, 0, 0, file_size); write_ptr = MapViewOfFile(write_handle, FILE_MAP_WRITE, 0, 0, file_size); // Необходимо доработать обработку невалидных хэндлов } ~memory_mapped_file() { if (read_handle != INVALID_HANDLE_VALUE && read_handle != nullptr) { UnmapViewOfFile(read_handle); CloseHandle(read_handle); } if (write_handle != INVALID_HANDLE_VALUE && write_handle != nullptr) { UnmapViewOfFile(write_handle); CloseHandle(write_handle); write_handle = nullptr; } } template <typename T> void write(const T data, const size_t offset = 0) { T* writable = static_cast<T*>(write_ptr); writable[offset] = data; } template <typename T> T read(const size_t offset = 0) { T* result = static_cast<T*>(write_ptr); return result[offset]; } HANDLE read_handle; HANDLE write_handle; void* read_ptr; void* write_ptr; // delete unusable operators and constructors as non-copy object memory_mapped_file(const memory_mapped_file&) = delete; memory_mapped_file(const memory_mapped_file&&) = delete; memory_mapped_file& operator=(const memory_mapped_file& other) = delete; memory_mapped_file&& operator=(const memory_mapped_file&& other) = delete; } mm_file; int main(int argc, char* argv[]) { mm_file mmf(sizeof(int) + sizeof(float)); mmf.write<int>(20); mmf.write(35.876f, sizeof(int)); std::cout << "mmf read result: (int = " << mmf.read<int>() << "), (float = " << mmf.read<float>(sizeof(int)) << ")" << std::endl; } 

    Such a structure will simplify your life, but you still need to manually calculate the offsets, you can think of some mechanism that would do the calculation for you.

    The result of the execution will be:

    mmf read result: (int = 20), (float = 35.876)

    enter image description here