在我的C#代碼,我有以下陣列:如何從C#雙陣列發送到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'。爲什麼要這麼做? – RocketR
這是C++/CLI,而不是「託管C++」。 –