C++ cli 错误C3698:&x27;CreerLevel::Mur^' ;:不可能的d';使用ce类型命令参数de';新潮';

C++ cli 错误C3698:&x27;CreerLevel::Mur^' ;:不可能的d';使用ce类型命令参数de';新潮';,c++-cli,C++ Cli,我已经创建了一个类,我需要将它与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() { ret

我已经创建了一个类,我需要将它与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;
 }
};
}
当我尝试使用它时

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

错误出现在文件“xmemory”的第52行,但我不知道这个文件xD

我同意Alexandre C。如果你想使用向量,你可以使用STL/CLR(http://msdn.microsoft.com/en-us/library/bb385954.aspx)vector。

编译器正在反对,因为您试图在非托管类中存储托管对象。这无法工作,垃圾收集器需要能够找到对象引用,以便能够正确地收集垃圾。由于它找不到非托管对象,因此也找不到托管引用

我强烈建议不要使用STL/CLR,它结合了STL和CLR的所有缺点。如果你真的,真的想使用vector,那么gcroot可以解决这个问题。但是,使用System::Collections::Generic::List是目前为止最好的解决方案

using namespace System::Collections::Generic;
...
  List<Mur^>^ vMurs = gcnew List<Mur^>;
...
  vMurs->Add(gcnew Mur(i, j));
使用命名空间System::Collections::Generic;
...
列表^vMurs=gcnew列表;
...
vMurs->Add(gcnew Mur(i,j));
尝试使用

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

我认为不允许在(非托管)向量中使用托管句柄。改用
array
。我强烈建议不要使用本地化的开发工具。在英语中使用它们意味着不必处理错误或糟糕的翻译,并且如果遇到问题(如现在),可以通过搜索引擎找到更多帮助。由于名称冲突,我在使用命名空间System::Collections::Generic时遇到了很多问题(即使不同时使用命名空间
System::Collections
)。如果您在使用此(优秀)解决方案时遇到问题,请记住仅使用命名空间System::Collections,并明确限定泛型集合(例如
generic::List^vMurs=gcnew generic::List
)我不喜欢无缘无故地混合托管类型和非托管类型。@Alexandre C:std::vector已经是非托管类型了……所以它们已经混合了……我的意思是我不喜欢不需要的
gcroot
。例如,我宁愿使用
List^
。你也不能将vMur放在托管类中。