【问题标题】:Using vector or some other methodology to keep track of class objects in C++ CLI [closed]使用向量或其他方法来跟踪 C++ CLI 中的类对象 [关闭]
【发布时间】:2019-02-03 03:50:46
【问题描述】:

我正在尝试以编程方式创建和跟踪对象,但无法在 C++ CLI 应用程序中实现。我尝试过使用向量和 CLI 等效的 cliext,这两者都引发了错误。在普通的 C++ 应用程序中,我使用 std::vector<Player> player(8) 创建一组对象并从中检索数据没有问题,Player 是类,player 是引用,(8) 是对象的数量.要存储或检索数据,我可以运行该类player[4].SetColor(22); 的方法。似乎 CLI 需要托管代码,cliext 应该处理它,但我在以这种方式运行时也遇到了错误:

Severity Code Description   Project File Line Suppression State
Error   C4484   'cliext::impl::vector_select<_Value_t,true>
::default::get': matches base ref class method 
'cliext::impl::vector_impl<_Value_t,true>::default::get',
but is not marked 'virtual', 'new' or 'override'; 'new'
(and not 'virtual') is assumed

和:

Severity Code Description Project File  Line Suppression State
Error C3673 'Player': class does not have a copy-constructor    

尽管如我所说,不用 CLI 也能正常运行。

任何 CLI 的帮助/示例将不胜感激,谢谢!

【问题讨论】:

  • Player 是否有复制构造函数或支持零规则?在我看来,编译器可能会试图警告你你是making a terrible mistake
  • 它没有,虽然我认为它创建了一个默认值。我试图添加一个,但这样做时看到另一个错误:an ordinary reference to a C++/CLI ref class or interface class is not allowed 。试图点击此链接:tutorialspoint.com/cplusplus/cpp_copy_constructor.htm
  • 当对象owns手动控制动态内存分配等资源时,默认的复制构造函数通常是无用的。我不太了解我的 CLI,无法帮助解决新错误。我建议添加您的代码,最好是 minimal reproducible example。 MCVE 的真正美妙之处在于,在创建一个时,您经常会暴露错误并且可以自己解决问题。
  • 这是我认为导致相同错误的最小项目。我什至不确定 CLI 是否/如何处理复制构造函数(尽管仍在调查):1drv.ms/u/s!Aj-jC988qcQojTNuY162Uwn5DZBo
  • 互联网上充斥着损坏或损坏的计算机,因为他们的用户点击了类似的链接。请把问题中的代码作为文本输入。

标签: c++ class object vector command-line-interface


【解决方案1】:

我能够使用array&lt; Player^ &gt;^ player = gcnew array&lt; Player^ &gt;(playerCount); 在 C++ CLI 中创建对象列表。之后,我可以为我需要的所有对象创建新玩家player[(the number of the player)] = gcnew Player;。参考:https://docs.microsoft.com/en-us/cpp/dotnet/how-to-use-arrays-in-cpp-cli?view=vs-2017

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-23
    • 2017-11-16
    • 1970-01-01
    • 2012-10-29
    • 1970-01-01
    • 2016-03-19
    • 1970-01-01
    相关资源
    最近更新 更多