我如何通过引用通用::名单?
-
22-08-2019 - |
题
在试图包裹在托管的.dll我试图数据点的Generic::List
转换成std::vector
一些非托管代码。这里是什么,我试图做一个片段:
namespace ManagedDLL
{
public ref class CppClass
{
void ListToStdVec( const List<double>& input_list, std::vector<double>& output_vector )
{
// Copy the contents of the input list into the vector
// ...
}
void ProcessData( List<double> sampleData )
{
std::vector<double> myVec;
ListToStdVec( sampleData, myVec );
// Now call the unmanaged code with the new vector
// ...
}
}
}
编译此给我:
错误C3699: '&':上式 'const的系统::类别::通用::列' 不能使用这种间接
我可能错过了一些东西在这里根本(我是比较新的做事.NET的方式),但看起来合理合法的代码给我..?
[编辑] 我都试过Andy和达里奥的建议和他们的工作,但我怎么然后访问输入列表的成员?我已经试过各种dreferencing并没有什么的组合看起来编译:
void ListToStdVec( const List<double>% input_list, std::vector<double>& output_vector )
{
int num_of_elements = input_list->Count;
}
void ListToStdVec( const List<double>^ input_list, std::vector<double>& output_vector )
{
int num_of_elements = input_list.Count;
}
...既给我:
错误C2662:“系统::收藏集::一般::表::数:得到”:不能从“常量系统::收藏集::一般::名单”到“系统转换“这个”指针::类别::通用::列表%'
...那么你如何访问引用/指针?
解决方案
作为List<T>
是管理.NET类,它是由管理通过GC-处理由^,而不是由C ++表示 - 参考
例如:
void ListToVec(List<double>^ input_list, std::vector<double>& out)
您不要在这里需要额外的const
。符号List<T>^%
创建一个跟踪参考(与C ++ - 指针)而不是通过引用的呼叫。
只是通过list->...
和list[...]
访问成员。
其他提示
根据香草萨特,%
是管理对象通由参考字符。代码转换为以下内容,它应该工作:
void ListToStdVec( const List<double>% input_list, std::vector<double>& output_vector
{
// Copy the contents of the input list into the vector
// ...
}
修改:我觉得const
是造成问题,但我不知道为什么。如果更改List
参数不const
,那么第一个函数将编译如果使用->
操作,而如果你使用.
运营商的第二功能将编译(我不知道为什么会存在差异 - 它不”吨多大意义)。
这是说,如果所有你想要做的是元素在List
复制到vector
,那么你真的想用^
。的那些认为作为具有向被管理对象的引用。我认为%
将被使用,如果你想通过“引用”的参考值(即重新分配input_list
到ListToStdVec()
内别的东西,并有来电看到,分配的结果。但是,由于您使用.
运营商访问成员使用%
时,即告诉我,我可能不理解的是,目的在所有。