对象中的对象列表

list of objects in an object

我是 C++ 的新手,这是我的第一个程序,所以可能有一些 odd/strange 代码对于 C++ 来说根本不是逻辑。

我正在使用 Visual studio 2015 C++/cli,我正在尝试获取另一个对象中的对象列表。

LinePiece.h:

ref class LinePiece
{
    private:
    std::string* Type;
    int ElementNr;
    int Status;
    int X, Y, X2, Y2;
    int Diameter;
    std::string* Text;
    public:
    LinePiece();

    LinePiece(std::string* a_type, int a_ElementNr, int a_Status, int a_x, int a_y, int a_x2, int a_y2)
    {
        Type = a_type;
        ElementNr = a_ElementNr;
        Status = a_Status;
        X = a_x;
        Y = a_y;
        X2 = a_x2;
        Y2 = a_y2;
    }
};

Element.h:

#include "LinePiece.h"
ref class Element
{
public:
    Element();

    //properties
    std::string* ON;                        //order nummer
    std::string* MO;                        //order merk
    std::string* SN;                        //element nummer
    std::string* RS;                        //element afwerking
    std::string* OW;                        //wapeningspatroon
    std::string* CN;                        //element calculation number
    int el_length;                          //element lengte
    int el_width;                           //element hoogte
    int el_beginX;                          //element beginpunt
    int el_concrete_height;                 //element hoogte beton
    int el_iso_height;                      //element isolatie hoogte
    std::string* el_weight;                 //element gewicht
    std::list<LinePiece^>* listLinePieces;  //lijst met contouren   
};

因此,当我尝试构建此代码时,出现以下错误:

'&&' 不能在类型 'LinePiece^'

上使用此间接寻址

如何解决这个问题,以便获得包含 LinePiece 个对象的对象列表?

您没有提到它,但您正在尝试使用某些特定的 C++/CX 功能,这是故意的吗?如果是这样,我认为您还需要添加一个命名空间,否则跳过 ref^.

代码太少,无法完全理解你想做什么,但我会跳过所有字符串的指针,我不认为那是你想做的,但因为我没有我无法确定您的其余代码。另外,制作列表指针可能也不是您想要的。

库 STL/CLR 包含在命名空间 cliext 而不是 std 中。

#include <cliext\list>
...
cliext::list <LinePiece^> listLinePieces;

我建议放弃指向 std::string 的指针,转而使用 .Net 中的 System::String^。

编辑:

.Net 还包括双向链表的实现。使用它:

#using <System.dll>
using namespace System::Collections::Generic;
...
LinkedList<LinePiece^> LinkedList;

阅读更多关于MSDN

  std::list<LinePiece^>* listLinePieces;

当你在罗马时,表现得像个罗马人就变得很重要。您不能将托管对象引用存储在非托管集合中。托管对象的存储由垃圾收集器负责,但它没有希望找回这些引用。它不知道有关 std::list 的内部结构的 bean,因此永远无法可靠地找到托管对象引用。编译器必须抱怨它。

改用List<LinePiece^>^。同样, 比 std::list 和 cliext::list 更有效。


  std::string* el_weight;

所有这些 std::string 指针都差不多,您可能发现了必须存储指向它们的指针的困难方法。 C++/CLI 编译器不允许将值存储在托管对象中,当垃圾收集器在压缩堆时移动对象时太危险了。而且你必须编写一个非常丑陋的构造函数来分配它们。现在可以正常工作,只要您实现了终结器(不要跳过!)。但是,当您将它们改为 String^ 时,在罗马的生活会容易得多。所以垃圾收集器会处理它们。