C++ 将 ref class 类型的变量列表发送到本机 class
C++ Send a variable List of type ref class to a native class
我基本有以下代码
//*** 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
*/
}
所以我的想法是调用 SomeViewerFunction,它将向 SNVT_List 添加数据。然后 SomeViewerFunction 将创建一个 Tools 实例,以调用 SomeToolsFunction。 SomeToolsFunction 采用变量 SNVT_List.
但是我得到一个错误; class "System::Collections::Generic::List" 没有合适的复制构造函数。
归根结底,我想要实现的是在查看器中创建一个 SNVT 类型的 System::Collections::Generic::List 类型变量,然后将该变量发送到工具。
关于如何实现这一目标的任何想法?
请注意以下...
我所有的 classes 都是 ref classes,class 工具除外。 Class 工具必须保持原生 class。
我知道这在 C# 中会更容易,但其他人在 C++ 中开始了这个项目,所以我必须在 C++ 中继续;
// 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
*/
}
List<Whatever^> Foo
这种类型不合适,几乎总是这样。无论是 class 成员、方法参数还是局部变量,将其切换为 List<Whatever^>^ Foo
,第二个 ^
。用 gcnew List<Whatever^>()
初始化它。 (注意:如果是值类型的列表,比如int
,<>
里面没有^
。)
List 是引用类型(在 C# 中为 class
,在 C++/CLI 中为 ref class
)。因此,它应该始终是对托管堆上对象的引用,这就是 ^
的意思。
如果没有 ^
,引用类型将直接放在对象内的 stack/allocated 上,这在 C++/CLI 中是可能的,但在 C# 中是不可能的。因此,none 的 API 需要该类型。这就是您收到 "List has no suitable copy constructor" 错误的原因,因为您正在尝试使用 List<Whatever^>
,但复制构造函数采用 List<Whatever^>^
.
另外,请注意这是不是 C++。这就是 C++/CLI,它具有 C++ 的所有复杂性、C# 的所有复杂性以及它自己的一些复杂性。
我基本有以下代码
//*** 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
*/
}
所以我的想法是调用 SomeViewerFunction,它将向 SNVT_List 添加数据。然后 SomeViewerFunction 将创建一个 Tools 实例,以调用 SomeToolsFunction。 SomeToolsFunction 采用变量 SNVT_List.
但是我得到一个错误; class "System::Collections::Generic::List" 没有合适的复制构造函数。
归根结底,我想要实现的是在查看器中创建一个 SNVT 类型的 System::Collections::Generic::List 类型变量,然后将该变量发送到工具。 关于如何实现这一目标的任何想法?
请注意以下... 我所有的 classes 都是 ref classes,class 工具除外。 Class 工具必须保持原生 class。 我知道这在 C# 中会更容易,但其他人在 C++ 中开始了这个项目,所以我必须在 C++ 中继续;
// 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
*/
}
List<Whatever^> Foo
这种类型不合适,几乎总是这样。无论是 class 成员、方法参数还是局部变量,将其切换为 List<Whatever^>^ Foo
,第二个 ^
。用 gcnew List<Whatever^>()
初始化它。 (注意:如果是值类型的列表,比如int
,<>
里面没有^
。)
List 是引用类型(在 C# 中为 class
,在 C++/CLI 中为 ref class
)。因此,它应该始终是对托管堆上对象的引用,这就是 ^
的意思。
如果没有 ^
,引用类型将直接放在对象内的 stack/allocated 上,这在 C++/CLI 中是可能的,但在 C# 中是不可能的。因此,none 的 API 需要该类型。这就是您收到 "List has no suitable copy constructor" 错误的原因,因为您正在尝试使用 List<Whatever^>
,但复制构造函数采用 List<Whatever^>^
.
另外,请注意这是不是 C++。这就是 C++/CLI,它具有 C++ 的所有复杂性、C# 的所有复杂性以及它自己的一些复杂性。