Kansuu

Hello,
sorry if this isn't the good forum to post my question to.
I have a STL/CLR vector called "vecteurMot":

Code Snippet

private: cliext::vector<mot^>^ vecteurMot;


then I do a pusback to test it :
Code Snippet

vecteurMot->push_back(gcnew mot("Mot FR : ", "Mot JP : ", "Ceci n'est qu'un exemple" ));


Everything compile, I run it then get a debugger error:
"Additional information: The referenced object isn't defined to an object instance" (That might be wrong, tried to translate from french to English)
Then I press Break, and the debugger show me those line :
Code Snippet

size_type size()
{ // return length of sequence
return (_Mysize);
}


Code Snippet

void push_back(value_type _Val)
{ // insert element at end
insert_n(size(), 1, _Val);
}


in the Vector file.

Hope you can help me.
Thanks.


Re: Visual C++ Language [C++/CLI] STL/CLR's Vector Problem.

Holger Grund

Sounds like a NullPointerException (dereferencing a null pointer or object variable). Have you initialized vecteurMot It will be initialized to nullptr by default.

-hg





Re: Visual C++ Language [C++/CLI] STL/CLR's Vector Problem.

Kansuu

Sorry i'm kinda new to STL container, how do we initialize it





Re: Visual C++ Language [C++/CLI] STL/CLR's Vector Problem.

Holger Grund

Handles (^) work similar to pointers in standard C++. vectorMot is an object variable that points to a given object. This is automatically initialized to nullptr, i.e. no actual object. You need to instantiate an object and assign it to the variable before you can use it as you want:

vectorMot = gcnew cliext::vector<...> ( ... );

or in the member initializer list:

vectorMot(gcnew cliext::vector<...>( ... ) )

An IMHO much more elegant and natural solution would be to declare vectorMot as cliext::vector and _not_ as a handle to it (just omit the ^). This does the gcnew and delete for you automatically and also lets you use object-like semantics to access members (i.e. . operator instead of -> etc.). The Microsoft folks seem to have a strange addiction to pointers, where good programming style would avoid them, so that some arbitrary limitations exist in the C++/CLI language binding. Nonetheless, it should just work in this case.

-hg





Re: Visual C++ Language [C++/CLI] STL/CLR's Vector Problem.

Kansuu

Thanks, now it work, but I get another debugger error at:

Code Snippet

ListViewItem ^item = gcnew ListViewItem(vecteurMot[i]->getMotFR());


It show me this in the vector file :

Code Snippet

virtual value_type get(size_type _Pos)
{ // get _Pos element
return (at(_Pos));
}


Code Snippet

reference at(size_type _Pos)
{ // subscript mutable sequence with checking
if (_Pos < 0 || size() <= _Pos)
throw gcnew System::ArgumentOutOfRangeException();

return (_Myarray[_Pos]);
}


Also I tried to omit the "^" but when I tried to compile it say error such as "left member of ".getMotFR" is not a structure or an union did you mean to use "->" instead

Thanks.

EDIT: Nevermind, I just made a mistake in my loop.