Class C++;将ref类类型的变量列表发送到本机类
我有下面的代码Class C++;将ref类类型的变量列表发送到本机类,class,c++-cli,Class,C++ Cli,我有下面的代码 //*** Field.h ***\\ public ref class Field { public: String^ Name; UInt16 Lenght; Field(void); } //*** SNVT.h ***\\ public ref class SNVT { public: String^ Name; UInt16 Index; List<Field^> FieldList; SNVT(vo
//*** Field.h ***\\
public ref class Field
{
public:
String^ Name;
UInt16 Lenght;
Field(void);
}
//*** SNVT.h ***\\
public ref class SNVT
{
public:
String^ Name;
UInt16 Index;
List<Field^> FieldList;
SNVT(void);
}
//*** Viewer.h ***\\
public ref class Viewer
{
public:
List<SNVT^> SNVT_List;
Viewer(void);
SomeViewerFunction();
}
//*** Viewer.cpp ***\\
void SomeViewerFunction()
{
SNVT^ currentSNVT = gcnew SNVT();
Field^ currentField = gcnew Field();
currentField->Name = "Field Name";
currentField->Leght = 10;
currentSNVT->Name = "SNVT Name";
currentSNVT->Index = 1;
currentSNVT->FieldList.Add(currentField);
SNVT_List.Add(currentSNVT);
Tools New_Tools;
New_Tools.SomeToolsFunction(SNVT_List);
}
//*** Tools.h ***\\
class Tools
{
public:
Tools(void);
SomeToolsFunction(List<SNVT^> Tools_SNVT_List);
}
//*** Tools.cpp ***\\
void SomeToolsFunction(List<SNVT^> Tools_SNVT_List)
{
/*
use Tools_SNVT_List here
*/
}
/**Field.h***\\
公共引用类字段
{
公众:
字符串^Name;
UInt16长度;
字段(无效);
}
//***SNVT.h***\\
公共参考类SNVT
{
公众:
字符串^Name;
UInt16指数;
列表字段列表;
SNVT(无效);
}
//***查看器.h***\\
公共引用类查看器
{
公众:
列表SNVT_列表;
观众(无效);
SomeViewerFunction();
}
//***Viewer.cpp***\\
void SomeViewerFunction()
{
SNVT^当前SNVT=gcnew SNVT();
字段^currentField=gcnew Field();
当前字段->名称=“字段名称”;
currentField->Leght=10;
currentSNVT->Name=“SNVT Name”;
currentSNVT->Index=1;
currentSNVT->FieldList.Add(当前字段);
SNVT_List.Add(当前SNVT);
工具新工具;
新工具。某些工具功能(SNVT列表);
}
//***工具***\\
类工具
{
公众:
工具(无效);
SomeTools功能(列出工具列表);
}
//***工具.cpp***\\
void SomeToolsFunction(列出工具列表)
{
/*
使用此处列出的工具
*/
}
所以我的想法是调用SomeViewerFunction,它将数据添加到SNVT_列表中。然后SomeViewerFunction将创建工具的实例,以调用SomeToolsFunction。SomeToolsFunction接受变量SNVT_List
但是我犯了一个错误;类“System::Collections::Generic::List”没有合适的副本构造函数
最后,我想要实现的就是在查看器中创建一个SNVT类型的System::Collections::Generic::List类型变量,然后将该变量发送给工具。
你知道我怎样才能做到这一点吗
请注意以下几点。。。
我所有的类都是ref类,除了类工具。类工具必须保持为本机类。
我知道C语言中的这一点会更容易,但是其他人在C++中启动了这个项目,所以我必须继续C++。p>
//2019-01-10
谢谢你,大卫·欧
我将代码更改为以下内容,并且它按照我的预期工作
//*** Field.h ***\\
public ref class Field
{
public:
String^ Name;
UInt16 Lenght;
Field(void);
}
//*** SNVT.h ***\\
public ref class SNVT
{
public:
String^ Name;
UInt16 Index;
List<Field^> FieldList;
SNVT(void);
}
//*** Viewer.h ***\\
public ref class Viewer
{
public:
List<SNVT^>^ SNVT_List; // I changed this line from List<SNVT^> SNVT_List;
Viewer(void);
SomeViewerFunction();
}
//*** Viewer.cpp ***\\
void SomeViewerFunction()
{
SNVT_List = gcnew List<SNVT^>(); // I added this line
SNVT^ currentSNVT = gcnew SNVT();
Field^ currentField = gcnew Field();
currentField->Name = "Field Name";
currentField->Leght = 10;
currentSNVT->Name = "SNVT Name";
currentSNVT->Index = 1;
currentSNVT->FieldList.Add(currentField);
SNVT_List->Add(currentSNVT); // I changed this line from SNVT_List.Add(currentSNVT);
Tools New_Tools;
New_Tools.SomeToolsFunction(SNVT_List);
}
//*** Tools.h ***\\
class Tools
{
public:
Tools(void);
SomeToolsFunction(List<SNVT^>^ Tools_SNVT_List); // I changed this line from SomeToolsFunction(List<SNVT^> Tools_SNVT_List);
}
//*** Tools.cpp ***\\
void SomeToolsFunction(List<SNVT^>^ Tools_SNVT_List) // I changed this line from void SomeToolsFunction(List<SNVT^> Tools_SNVT_List)
{
/*
use Tools_SNVT_List here
*/
}
/**Field.h***\\
公共引用类字段
{
公众:
字符串^Name;
UInt16长度;
字段(无效);
}
//***SNVT.h***\\
公共参考类SNVT
{
公众:
字符串^Name;
UInt16指数;
列表字段列表;
SNVT(无效);
}
//***查看器.h***\\
公共引用类查看器
{
公众:
List^SNVT\u List;//我从List SNVT\u List更改了此行;
观众(无效);
SomeViewerFunction();
}
//***Viewer.cpp***\\
void SomeViewerFunction()
{
SNVT_List=gcnew List();//我添加了这一行
SNVT^当前SNVT=gcnew SNVT();
字段^currentField=gcnew Field();
当前字段->名称=“字段名称”;
currentField->Leght=10;
currentSNVT->Name=“SNVT Name”;
currentSNVT->Index=1;
currentSNVT->FieldList.Add(当前字段);
SNVT_List->Add(currentSNVT);//我从SNVT_List.Add(currentSNVT)更改了此行;
工具新工具;
新工具。某些工具功能(SNVT列表);
}
//***工具***\\
类工具
{
公众:
工具(无效);
SomeToolsFunction(List^Tools\u SNVT\u List);//我从SomeToolsFunction(List Tools\u SNVT\u List)更改了此行;
}
//***工具.cpp***\\
void SomeToolsFunction(List^Tools\u SNVT\u List)//我更改了void SomeToolsFunction(List Tools\u SNVT\u List)的此行
{
/*
使用此处列出的工具
*/
}
列出Foo
这种类型几乎总是不合适的。无论它是类成员、方法参数还是局部变量,都将其切换到List^Foo
,第二个^
。使用gcnew List()
初始化它。(注意:如果它是一个值类型的列表,例如int
,则在
中没有^
)
列表是引用类型(C#中的class
,C++/CLI中的ref class
)。因此,它应该始终是对托管堆上对象的引用,这就是^
的含义
如果没有^
,引用类型将直接放在堆栈上/在对象内分配,这在C++/CLI中是可能的,但在C#中是不可能的。因此,没有一个API需要这种类型。这就是为什么会出现“列表没有合适的复制构造函数”错误的原因,因为您试图使用List
,但是复制构造函数会使用List^
也注意到这不是C++。这是C++ + CLI,它具有C++的所有复杂性,C的所有复杂性,以及它自己的一些复杂性。p> 谢谢David Yaw还有一个我不明白的问题。为什么我不能在工具类中有一个列表SNVTHELIST,或者在Toels.CPP文件中有一个全局列表^ SNVTTHELISH?你的<代码>工具< /Cord>类是C++本地代码,而<代码>列表^ < /Cord>是来自不同世界管理的.NETFramework世界的对象。伟大的C++/CLI是为了轻松处理这两个世界而创建的。因此,在托管
ref
C++/CLI类中,您可以将本机代码和托管代码结合起来,但在本机Tools
类中,处理托管对象并不是那么容易。
List<Whatever^> Foo