正如标题所说,我必须创建可以接受数组作为参数的类。
这是我目前版本的头文件:
public ref class MyClass { public: MyClass() {}; MyClass(array<int, 2> ^(&A1), const int &i2) : A1(A1), I2(i2) {}; String^ Method(); ~MyClass() {}; private: array<int, 2>^ A1 = gcnew array<int, 2>(3, 3) { { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 }, }; int I2 = 5; }; String^ MyClass::Method() // Simple output for debugging { String^ OutputText; int sum=10; OutputText= "OutputText = " + sum; return OutputText; }截至目前,我收到以下错误:
'$ S1':全局或静态变量可能没有托管类型'cli :: array ^'
如果我将我的数组更改为静态,我会得到:
“A1”不是非静态数据成员或类“MyClass”的基类
类必须有两个构造函数。 我可以接受矢量解决方案,但我遇到的问题几乎与它相同。
As the title says I have to create class that can accept arrays as a parameter.
Here is my current version of the header file:
public ref class MyClass { public: MyClass() {}; MyClass(array<int, 2> ^(&A1), const int &i2) : A1(A1), I2(i2) {}; String^ Method(); ~MyClass() {}; private: array<int, 2>^ A1 = gcnew array<int, 2>(3, 3) { { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 }, }; int I2 = 5; }; String^ MyClass::Method() // Simple output for debugging { String^ OutputText; int sum=10; OutputText= "OutputText = " + sum; return OutputText; }As of now I'm getting the following error:
'$S1': global or static variable may not have managed type 'cli::array ^'
If I change my array to static I'll get:
"A1" is not a nonstatic data member or base class of class "MyClass"
Class has to have both constructors. I can accept a solution with vector, but i experience pretty much the same issues with it.
最满意答案
下面是我在将数组的初始化移动到构造函数中的含义:
public ref class MyClass { public: MyClass() { A1 = gcnew array<int, 2>(3, 3) { { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 }, }; }; MyClass(array<int, 2> ^(&A1), const int &i2) : A1(A1), I2(i2) {}; String^ Method(); ~MyClass() {}; private: array<int, 2>^ A1; int I2 = 5; };Here's what I meant under moving the initialization of the array to a constructor:
public ref class MyClass { public: MyClass() { A1 = gcnew array<int, 2>(3, 3) { { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 }, }; }; MyClass(array<int, 2> ^(&A1), const int &i2) : A1(A1), I2(i2) {}; String^ Method(); ~MyClass() {}; private: array<int, 2>^ A1; int I2 = 5; };更多推荐
发布评论