我已经创建了一个类,我需要将它与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));在文件"xmemory“的第52行出现错误,但我不知道这个文件xD
发布于 2011-01-03 23:12:25
我同意Alexandre C。如果你想使用向量,你可以使用STL/CLR (http://msdn.microsoft.com/en-us/library/bb385954.aspx)向量。
发布于 2011-01-03 23:26:54
编译器反对,因为您试图将托管对象存储在非托管类中。这是行不通的,垃圾收集器需要能够找到对象引用,这样它才能正确地收集垃圾。由于它找不到非托管对象,因此也找不到托管引用。
我强烈建议不要使用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));发布于 2011-01-04 04:17:45
尝试使用
std::vector<gcroot<Mur ^> > vMurs;
...
vMurs.push_back(gcnew Mur(i,j));https://stackoverflow.com/questions/4585192
复制相似问题