For a class project I am trying to make a dynamic array of pointers to the base class and program keeps crashing. I have the base class objectType, and the derived classes balloonType, enemyType, and obstacleType. I define the array of pointers and allocate memory like this.
this however is not where it crashes, it crashes when the base class function GetDepth() is called. The function is a member of objectType, here is the function definition and where it crashesCode:objectType **m_object; m_object = new objectType*[m_nObjects]; //load the objects for (int i = 0; i < m_nObjects; i++) { string itemType; inFile >> itemType; if (itemType == "balloon") m_object[i] = new balloonType; if (itemType == "obstacle") m_object[i] = new obstacleType; if (itemType == "enemy") m_object[i] = new enemyType; inFile >> *m_object[i]; }objectType **m_object;
Code://inside objectType class inline int GetDepth() { return m_depth; } void worldManager::DepthSort() { //bubble sort by depth - high to low for (int i = 0; i < (m_nObjects - 1); i++) { for (int j = 0; j < (m_nObjects - 1 - i); j++) { if (m_object[j]->GetDepth() < m_object[j+1]->GetDepth()) //******* crashes here, it says that it //cannot access memory for GetDepth() { objectType *tempObj = m_object[j]; m_object[j] = m_object[j + 1]; m_object[j + 1] = tempObj; } } } }



LinkBack URL
About LinkBacks


