【发布时间】:2011-07-26 21:14:08
【问题描述】:
在我的 C# 代码中,我有以下数组:
var prices = new[] {1.1, 1.2, 1.3, 4, 5,};
我需要将它作为参数传递给我的托管 C++ 模块。
var discountedPrices = MyManagedCpp.GetDiscountedPrices(prices) ;
GetDiscountedPrices 的签名应该是什么样子的?最琐碎的情况下,当折扣价等于价格时,C++方法GetDiscountedPrices应该是怎样的?
编辑:我设法让它编译。我的 C# 代码是这样的:
[Test]
public void test3()
{
var prices = new ValueType[] {1.1, 1.2, 1.3, 4, 5,};
var t = new TestArray2(prices , 5);
}
我的 C++ 代码构建:
TestArray2(
array<double^>^ prices,int maxNumDays)
{
for(int i=0;i<maxNumDays;i++)
{
// blows up at the line below
double price = double(prices[i]);
}
但是我遇到了运行时错误:
System.InvalidCastException : 指定的强制转换无效。
编辑:凯文的解决方案奏效了。我还找到了一个有用的链接:C++/CLI keywords: Under the hood
【问题讨论】:
-
为什么
^在double^中?很明显,您不能将double^转换为double。你为什么要这样做? -
这是 C++/CLI,而不是“托管 C++”。