用 fnc 类内的数据填充向量

发布于 2025-01-02 22:31:11 字数 790 浏览 1 评论 0原文

我有一个自定义数据类型数组和向量,如下所示。在 Foo 函数中,我开始用数据填充数组和向量。无论如何,用数据填充数组没有问题。但是我无法使用矢量访问任何内容。我找不到我所缺少的东西。

有没有办法用数据填充矢量对象。

// MyClass.h
#include <cliext/vector>
using namespace System;
using namespace cliext;

public ref class MyClass {

private :
static int x ;
static float y ;
String ^name;

public :
static array<MyClass ^> ^myArray = gcnew array <MyClass^> (3) ;
static vector<MyClass^> ^myVector = gcnew vector <MyClass^> (3) ;

void Foo();

};

// MyClass.cpp
#include "stdafx.h"
#include <MyClass.h>

void MyClass::Foo()
{
myArray[0] = gcnew MyClass;
myVector[0] = gcnew MyClass;

myArray[0]->x = 100 ;
myArray[0]->x = 99.5 ;
myArray[0]->name = "Good" ;

myVector[0]->CAN'T ACCESS ANY CLASS DATA MEMBER !!

}

I have a custom data type array and vector as below. In Foo function i started to fill array and vector with data. Anyway there was no problem filling array with data. However i couldn't access anything with vector. I could not find what i am missing .

Is there a way to fill the vector objects with data.

// MyClass.h
#include <cliext/vector>
using namespace System;
using namespace cliext;

public ref class MyClass {

private :
static int x ;
static float y ;
String ^name;

public :
static array<MyClass ^> ^myArray = gcnew array <MyClass^> (3) ;
static vector<MyClass^> ^myVector = gcnew vector <MyClass^> (3) ;

void Foo();

};

// MyClass.cpp
#include "stdafx.h"
#include <MyClass.h>

void MyClass::Foo()
{
myArray[0] = gcnew MyClass;
myVector[0] = gcnew MyClass;

myArray[0]->x = 100 ;
myArray[0]->x = 99.5 ;
myArray[0]->name = "Good" ;

myVector[0]->CAN'T ACCESS ANY CLASS DATA MEMBER !!

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

权谋诡计 2025-01-09 22:31:11

以下是 MSDN 解释发生的情况:如何:从程序集中公开 STL/CLR 容器

“列表和映射等 STL/CLR 容器被实现为模板引用类。由于 C++ 模板是在编译时实例化的,因此具有完全相同的签名但位于不同程序集中的两个模板类是实际上是不同的类型。这意味着模板类不能跨程序集边界使用。”

据我了解,您的公共类正在尝试导出向量的模板专业化,但这将具有与同一向量的外部声明不同的签名,并且永远不会匹配。

您可能想像这样更改 myVector 元素(为我编译):

static cliext::vector<MyClass^>::generic_container ^myVector = gcnew cliext::vector<MyClass^>(3); 

另一个选择是不将您的类标记为“公共”,这样编译器就不会尝试使其在程序集之外可用。

我还会注意到,在 x 和 y 上使用“static”对我来说似乎很可疑。您确定只想要其中之一吗?

Here's the MSDN explaining what's happening: How to: Expose an STL/CLR Container from an Assembly

"STL/CLR containers such as list and map are implemented as template ref classes. Because C++ templates are instantiated at compile time, two template classes that have exactly the same signature but are in different assemblies are actually different types. This means that template classes cannot be used across assembly boundaries."

As I understand it, your public class is attempting to export a template specialization of vector but this will have a different signature from an external declaration of the same vector and would never match.

You may want to change the myVector element like this (which compiles for me):

static cliext::vector<MyClass^>::generic_container ^myVector = gcnew cliext::vector<MyClass^>(3); 

Another option is to not mark your class as 'public' so the compiler doesn't attempt to make it usable outside of your assembly.

I'll also note that using 'static' on x and y seems suspect to me. You sure you only want one of those?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文