List 对象中的对象列表 我是C++的新手,这是我的第一个程序,所以可以有一些奇怪的代码,这对于C++来说根本不是逻辑。p>
我正在使用Visual studio 2015 C++/cli,并尝试在另一个对象中获取对象列表 线条。h:List 对象中的对象列表 我是C++的新手,这是我的第一个程序,所以可以有一些奇怪的代码,这对于C++来说根本不是逻辑。p>,list,object,c++-cli,List,Object,C++ Cli,我正在使用Visual studio 2015 C++/cli,并尝试在另一个对象中获取对象列表 线条。h: ref class LinePiece { private: std::string* Type; int ElementNr; int Status; int X, Y, X2, Y2; int Diameter; std::string* Text; public: LinePiece(); LineP
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;
}
};
要素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.h”
参考类元素
{
公众:
元素();
//性质
std::string*ON;//订单编号
std::string*MO;//订单merk
std::string*SN;//元素numer
std::string*RS;//元素afwerking
std::string*OW;//wapeningsparoon
std::string*CN;//元素计算编号
int el_length;//元素长度
int el_width;//元素hoogte
int el_beginX;//元素beginput
int el_混凝土高度;//元素hoogte beton
int el_iso_height;//元素隔离hoogte
std::string*el_weight;//元素gewicht
std::list*listLinePieces;//lijst met轮廓线
};
因此,当我尝试构建此代码时,会出现以下错误:
“&&”无法在类型“LinePiece^”上使用此间接寻址
如何解决此问题,以便获得带有
线条对象的对象列表?您没有提到它,但您正在尝试使用一些特定的C++/CX功能,这是故意的吗?如果是这样,我认为您也需要添加名称空间,否则跳过ref
和^
代码太少,无法完全理解您想要做什么,但我会跳过所有字符串的指针,我不认为这是您想要做的,但由于我没有您的其余代码,我不能确定。另外,创建列表指针可能也不是您想要的。库STL/CLR包含在命名空间cliext中,而不是std中
#include <cliext\list>
...
cliext::list <LinePiece^> listLinePieces;
#包括
...
cliext::列出列表行;
我建议放弃指向std::string的指针,转而使用.Net中的System::string^
编辑:
Net还包括双链接列表的实现。要使用它:
#using <System.dll>
using namespace System::Collections::Generic;
...
LinkedList<LinePiece^> LinkedList;
#使用
使用命名空间System::Collections::Generic;
...
LinkedList LinkedList;
阅读更多关于
对于所有这些std::string指针来说,情况大致相同,您可能发现了存储指向它们的指针的困难方法。C++/CLI编译器不允许将值存储在托管对象中,当垃圾收集器在压缩堆时移动对象时,这太危险了。你必须编写一个非常难看的构造函数来分配它们。只要实现了终结器(不要跳过!),现在就可以了。但是当你把它们变成一根绳子时,罗马的生活就容易多了。所以垃圾收集器会处理它们。这就是我目前得到的所有信息,稍后当我可以创建这个列表时,我想通过读取一个文件来填充它,该文件提供了这些类的信息。例如,这个文件是由元素构建的,我通过读取这些行(例如,文件中引用的ordernumber为ON)从文件中获取这些信息,然后有一些行表示CO X/Y开始值,X/Y结束值。这是一个线条对象。当我实际删除ref
和^
时,它不会给出任何错误。那么这是如何工作的呢?我习惯了C#,以前从未使用过这个图标。我想最大的问题是,你在项目中使用.NET的东西吗。如果您不这样做,我会将其转换为使用STL,如果您使用上面的Hans描述。当我使用列表^listLinePieces
时,它会出现以下错误:“^”:无法在类型std::List>
上使用此间接寻址,并且不允许使用非托管类的句柄
您只完成了一半的工作。您必须完全摆脱std::list,同时更改声明。我应该包括什么来识别list
?将类声明中的std::list*
替换为list^
。并使用listLinePieces=gcnewlist在构造函数中初始化它
Put使用名称空间System::Collections::Generic代码>位于源代码文件的顶部,以便编译器知道列表的含义。有很多很多罗马地图,你会从中受益匪浅。啊,是的,我注意到了。效果更好,对我来说听起来更符合逻辑(对我来说,^
符号仍然很奇怪,因为我习惯了C)。但是我更喜欢System::String
,因为它们可以与控件通信,而无需首先转换控件。谢谢您的信息和帮助@HansPassant
std::list<LinePiece^>* listLinePieces;
std::string* el_weight;