c++ - 转换 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
解决方案
它应该是
std::unique_ptr<CHAR_INFO []> screenBuffer;
screenBuffer = std::make_unique<CHAR_INFO[]>(consoleWidth * consoleHeight);
我添加[]
你需要一个数组。
我放下const
,所以你可以填补它。
std::vector<CHAR_INFO> screenBuffer(consoleWidth * consoleHeight);
是另一种选择。
推荐阅读
- php - 如何在 Laravel 中读取 excel 数据?
- docker - 在没有指定奇异性的情况下使用奇异性在snakemake中运行docker管道 exec docker://
- angular - nginx 和 kubernetes 背后的 Angular 黑页
- c# - 只有一个 TextBlock 的用户控件。如何向消费者公开 TextWrapping 属性?
- html - 初学者 HTML/CSS 练习
- excel - 记住公式的前一个值
- windows - 在 windows cmd 中使用反斜杠
- python - 为什么我的 python 代码输出错误的东西?
- go - Goroutine 没有按预期运行
- c++ - 用于 ubuntu linux 的 conio.h