我有一段代码,类似于以下内容:双变量定义2D CLI阵列长度失败
double dTest1, dTest2;
int iTest1, iTest2;
dTest1 = 15.0;
dTest2 = 20.0;
array<int^,2>^ arr2Test = gcnew array<int^,2>(dTest1, dTest2);
iTest1 = arr2Test->GetLength(0);
iTest2 = arr2Test->GetLength(1);
2D阵列的长度是可变的,并且所述长度信息存储在2个双变量。原来不行:
iTest1 = 1077149696
iTest2 = 0
这里怎么回事?编译器或解释器不能使用双变量来存储数组长度吗?
其实它工作时,我有一维数组:
array<int^>^ arrTest = gcnew array<int^>(dTest1);
iTest1 = arrTest->GetLength(0);
--> iTest1 = 15
上面的问题的解决方案是一个明确的转换成int,这本来也应该做,但也被人遗忘(如果你不要给编译器警告该死的):
array<int^,2>^ arr2Test = gcnew array<int^,2>((int)dTest1, (int)dTest2);
C++/CLI的两种基本生存策略。一个是何时知道要使用帽子^,int是一个值类型,不应该写成int ^。另一种是如何知道对它提出问题,“没有工作”是一个糟糕的问题描述。当然,数组具有整数个元素,1.5个元素没有多大意义。编写有意义的代码并消除许多问题。 –