如何通过引用传递 Generic::List?
为了将一些非托管代码包装在托管 .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 System::Collections::Generic::List”上使用此间接寻址
我可能在这里错过了一些基本内容(我对 .net 的处理方式相对较新),但这看起来像是相当有效的代码大部头书.. ?
[编辑] 我已经尝试了 Andy 和 Dario 的建议并且它们有效,但是我如何访问输入列表的成员? 我已经尝试了各种 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:“System::Collections::Generic::List::Count::get”:无法将“this”指针从“const System::Collections::Generic::List”转换为“System::”集合::通用::列表%'
...那么如何访问引用/指针?
In an attempt to wrap some unmanaged code in a managed .dll I'm trying to convert a Generic::List
of data points into a std::vector
. Here's a snippet of what I'm trying to do:
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
// ...
}
}
}
Compiling this gives me:
error C3699: '&' : cannot use this indirection on type 'const System::Collections::Generic::List'
I've probably missed something fundamental here (I'm relatively new to .net's way of doing things), but that looks like reasonably valid code to me.. ?
[Edit] I've tried both Andy and Dario's suggestions and they work, but how do I then access the members of the input list? I've tried all sorts of combinations of dreferencing and nothing seems to compile:
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;
}
...both give me:
error C2662: 'System::Collections::Generic::List::Count::get' : cannot convert 'this' pointer from 'const System::Collections::Generic::List' to 'System::Collections::Generic::List %'
...so how do you access the reference / pointer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 Herb Sutter,
%< /code> 是通过引用传递的托管对象字符。 将代码转换为以下内容,它应该可以工作:
编辑:我认为
const
导致了问题,尽管我不确定原因。 如果将List
参数更改为不是const
,则使用->
运算符时第一个函数将编译,而第二个函数将编译如果您使用.
运算符,函数将编译(我不确定为什么存在这种差异 - 它没有多大意义)。也就是说,如果您只想将
List
中的元素复制到vector
,那么您确实想使用^
。 可以将其视为对托管对象的引用。 我认为如果您想“通过引用”传递引用(即将input_list
重新分配给ListToStdVec()
中的其他内容,则可以使用%
,并让调用者看到该赋值的结果但是,考虑到您在使用%
时使用.
运算符来访问成员,这告诉我我可能不理解。这根本就是目的。According to Herb Sutter,
%
is the managed object pass by reference character. Convert the code to the following, and it should work:Edit: I think the
const
is causing the issues, although I'm not sure why. If you change theList
argument to not beconst
, then the first function will compile if you use the->
operator, while the second function will compile if you use the.
operator (I'm not sure why that difference exists - it doesn't make much sense).That said, if all that you want to do is to copy the elements in the
List
to thevector
, then you really want to use^
. Think of that as having a reference to the managed object. I think that%
would be used if you want to pass the reference "by reference" (i.e. reassigninput_list
to something else withinListToStdVec()
, and have the caller see the result of that assignment. However, given that you use the.
operator to access members when using%
, that tells me that I may not understand the purpose of that at all.由于
List
是托管 .NET 类,因此它由 ^ 表示的托管 GC-Handle 传递,而不是通过 C++ 引用传递。例如:
这里不需要额外的
const
。 符号List^%
创建跟踪引用(与 C++ 指针相当),而不是通过引用进行调用。只需通过
list->...
和list[...]
访问成员即可。As
List<T>
is a managed .NET class, it's passed by managed GC-Handle denoted by ^ and not by C++-reference.Ex:
You don't need additional
const
here. The notationList<T>^%
creates a tracking reference (comparable to C++-pointers) rather than a call by reference.Just access the members by
list->...
andlist[...]
.