【问题标题】:Cannot add to a c++cli collections::generic::list无法添加到 c++cli collections::generic::list
【发布时间】:2014-08-26 01:00:23
【问题描述】:

我做了一个这样的简单树类:

public ref class SimpleTreeNode {
public:
    String^ Data;
    List<SimpleTreeNode^>^ Children = gcnew List<SimpleTreeNode^>;
}

然而,当我去尝试添加这样的东西时:

auto^ nstn = gcnew SimpleTreeNode();
nstn->Children->Add(gcnew SimpleTreeNode());

它说“错误:函数“System::collections::generic::List::Add [with T=SimpleTreeNode^]”不能用给定的参数列表调用

参数类型有:(SimpleTreeNode^) 对象类型为:System::Collections::Generic::List^"

我在这里错过了什么?这应该可行,不是吗?

【问题讨论】:

  • 是的,我认为它应该可以工作。
  • 但它完全没有。我最终向它添加了一个方法“AddChild(SimpleTreeNode^ n) {Children->Add(n)}”并且有效。

标签: c++-cli


【解决方案1】:

对于您的情况,您应该对 ref 和 value 类型都使用不带帽子的关键字 auto。像这样:

auto nstn = gcnew SimpleTreeNode();
auto cN = nstn->Children->Count;

nstn->Children->Add(gcnew SimpleTreeNode());
auto cN2 = nstn->Children->Count;

【讨论】:

    猜你喜欢
    • 2022-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-26
    • 2017-11-19
    相关资源
    最近更新 更多