Reputation: 11
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 !!
}
Upvotes: 0
Views: 339
Reputation: 4986
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?
Upvotes: 1