开发者

error C3698: 'CreerLevel::Mur ^' : impossible d'utiliser ce type comme argument de 'nouveau'

开发者 https://www.devze.com 2023-02-02 00:01 出处:网络
i have create one class and i need to use it with vector. ref class Mur { public: int debutX, debutY; int finX, finY;

i have create one class and i need to use it with vector.

    ref class Mur
{
public:
 int debutX, debutY;
 int finX, finY;
 Mur (){}
 Mur(int debutX, int debutY) {
  this->debutX = debutX;
  this->debutY = debutY;
  finX = 0;
  finY = 0;
 }
 ~Mur()
  {
  }
 int getX() { return debutX; }
 int getY() { return debutY; }

 bool estFinit() {
  return (finX==0);
 }

 void finir(int x, int y){
  finX = x;
  finY = y;
 }
开发者_Python百科};
}

When i try to use it

 std::vector<Mur^> vMurs;
...
  vMurs.push_back(gcnew Mur(i,j));

Error come in file "xmemory" at line 52 but i don't know this file xD


The compiler is objecting because you're trying to store a managed object in an unmanaged class. That cannot work, the garbage collector needs to be able to find object references so it can properly collect garbage. And since it cannot find unmanaged objects, it cannot find the managed reference either.

I'd strongly advice to not use STL/CLR, it combines all the disadvantages of STL with those of the CLR. If you really, really want to use vector<> then gcroot<> can solve the problem. However, using System::Collections::Generic::List<> is by far the best solution.

using namespace System::Collections::Generic;
...
  List<Mur^>^ vMurs = gcnew List<Mur^>;
...
  vMurs->Add(gcnew Mur(i, j));


I agree with Alexandre C. If you want to use a vector, you could use the STL/CLR (http://msdn.microsoft.com/en-us/library/bb385954.aspx) vector.


Try using

std::vector<gcroot<Mur ^> > vMurs;
...
vMurs.push_back(gcnew Mur(i,j));
0

精彩评论

暂无评论...
验证码 换一张
取 消