|
-
February 5th, 2012, 07:32 PM
#1
Vector
Hi codeguru ,
I have 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.
Please help, urgent !!
<CODE
// MyClass.h
#include <cliext/vector>
using namespace cliext;
using namespace System;
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 !!
}
/CODE>
Last edited by ey_dsl; February 5th, 2012 at 09:22 PM.
-
February 6th, 2012, 09:59 AM
#2
Re: Vector
Your MyClass.cpp didn't even compile (after changing the last line of the function body into something with valid syntax, of course) since the header file couldn't be found until I changed #include <MyClass.h> to #include "MyClass.h". Perhaps that worked for you because you set up your project source file directory as an include directory in your project settings or even VS configuration, but unless MyClass is some sort of library module this is usually not the way to go.
Aside from that the only weird thing I found is that the members MyClass::x and MyClass::y are static, yet you syntactically access them like instance members. This is valid, but that way the concrete object represented by the identifier to the left of the -> is ignored, only its class is considered. This leads to the (seemingly) weird behavior that the values of the members x and y are always the same across all instances of MyClass.
Your code tags are wrong, BTW.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
February 6th, 2012, 11:49 AM
#3
Re: Vector
Try not to mix .NET and native C++ constructs like this.
Vector is a native C++ container and in .NET you should be using the .NET equivalents : System.Collections.Generic.List for instance.
Regardless native C++ types should not be gcnewed so the following is wrong :
Code:
static vector<MyClass^> ^myVector = gcnew ...
Just as a rule of thumb anything which is stl is a native C++ type (i.e. namespace std).
And don't use anything in cliexp unless you know the difference between .NET types and native C++ types.
Darwen.
P.S. <edit> Just had a look at cliext and vector is declared as a ref class. However I still think you should use the standard .NET collections rather than these.
Last edited by darwen; February 6th, 2012 at 11:55 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|