C ++ CLI将数组传递给类(C++ CLI Passing an array to class)

系统教程 行业动态 更新时间:2024-06-14 16:58:30
C ++ CLI将数组传递给类(C++ CLI Passing an array to class)

正如标题所说,我必须创建可以接受数组作为参数的类。

这是我目前版本的头文件:

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; };

更多推荐

本文发布于:2023-04-15 03:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/cc101cb8e7fb424cf840bf7e6c5fa39f.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   CLI   Passing   class   array

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!