如何创建一个类对象向量的向量

时间:2016-11-18 11:14:00

标签: c++ vector cocos2d-x

我使用的是Cocos 2d-x,我需要创建一个类的对象向量,但是当我尝试创建类型错误时,我得到了类型错误:

header
Vector<Vector<Level*> > _stagesLevelsVec;
//Here's the type Error when I try to create a Vector of Vectors of Objects of Class Level
//Error: Invalid Type for cocos2d::Vector<T>!
//Last output line: see reference to class template instantiation 'cocos2d::Vector<cocos2d::Vector<Level *>>' being compiled

CPP

Level * level_0 = new Level; //I have to create an Object of Class Level
level_0->setLevel(0); //I have to set a Property of the Object using a Method of the Class.

Vector<Level *> allLevelsVec_0; //Here's the type Error when I try to create a Vector of Objects Level
//Error: Invalid Type for cocos2d::Vector<T>!
//Last output line: see reference to class template instantiation 'cocos2d::Vector<Level *>' being compiled

allLevelsVec_0.pushBack(level_0); //I have to add the Object into the Vector
_stagesLevelsVec.pushBack(allLevelsVec_0); //I have to add a Vector of Objets Level into a Vector

//Then somewhere in code

auto allLevelsVec = _stagesLevelsVec.at(0); // I have to get the Vector of Objects Level that I need
auto level = allLevelsVec.at(0); //I have to get the Object Level
auto levelId = level->getLevel(); //Finally I have to get the Property of level using a Method of the Class

CCLOG("This is level: %i", levelId); //Output must be: This is level: 0.    

感谢您就此事提供任何指导。问候。

1 个答案:

答案 0 :(得分:2)

阅读doc,您似乎尝试了禁止声明:

  

模板参数

     

T - 元素的类型。

     

T必须是指向cocos2d :: Ref后代对象类型的指针。不允许使用其他数据类型或原语,因为我们已将cocos2d-x的内存管理模型集成到cocos2d :: Vector中。 (自v3.0测试版)

您不能将cocos2d::Vector用作T类型。