到 const CHAR_INFO*,c++,pointers,unique-ptr"/>

首页 > 解决方案 > 转换 unique_ptr到 const CHAR_INFO*

问题描述

从这里我知道 WriteConsoleOutput() 函数需要一个 const CHAR_INFO * 参数,我试图弄清楚是否有办法让我使用我提供的代码或者我是否必须使用原始指针? https://docs.microsoft.com/en-us/windows/console/writeconsoleoutput 该错误告诉我,我需要转换我提供的类型或放弃这个想法。我尝试过强制转换,但最终无济于事......我是智能指针的新手,所以如果那里有解释,对不起。

std::unique_ptr<const CHAR_INFO> screenBuffer;
screenBuffer = std::make_unique<const CHAR_INFO>(consoleWidth * consoleHeight);
WriteConsoleOutput(hConsole, screenBuffer.get(), { (short)consoleWidth * (short)consoleHeight }, { 0,0 }, &consoleSmallRect);
Severity    Code    Description Project File    Line    Suppression State
Error   C2664   '_CHAR_INFO::_CHAR_INFO(_CHAR_INFO &&)': cannot convert argument 1 from '_Ty' to 'const _CHAR_INFO &'   Snek    C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\include\memory  2055    

标签: c++pointersunique-ptr

解决方案


它应该是

std::unique_ptr<CHAR_INFO []> screenBuffer;
screenBuffer = std::make_unique<CHAR_INFO[]>(consoleWidth * consoleHeight);

我添加[]你需要一个数组。
我放下const,所以你可以填补它。

std::vector<CHAR_INFO> screenBuffer(consoleWidth * consoleHeight);是另一种选择。


推荐阅读