【问题标题】:Converting unique_ptr<const CHAR_INFO> to const CHAR_INFO*将 unique_ptr<const CHAR_INFO> 转换为 const CHAR_INFO*
【发布时间】:2020-03-25 17:08:44
【问题描述】:

据此,我知道 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++ pointers unique-ptr


    【解决方案1】:

    应该是

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

    我添加了[],因为您需要一个数组。
    我删除const,以便您填写。

    std::vector&lt;CHAR_INFO&gt; screenBuffer(consoleWidth * consoleHeight); 是另一种选择。

    【讨论】:

      猜你喜欢
      • 2020-12-14
      • 2015-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-25
      • 2011-08-16
      相关资源
      最近更新 更多