gpt4 book ai didi

c++ - vector 下标超出范围 - 更新方法

转载 作者:太空宇宙 更新时间:2023-11-04 11:39:57 25 4
gpt4 key购买 nike

我有一个错误。

“vector 下标超出范围,第 932 行。”

我有两种敌人类型,一组小行星,然后是一个单独的敌人。它们的制作完全相同,只是小行星的数量是多倍的,而博格是独立的。我已将错误追踪到 Borg 的更新方法,该方法与 Asteroids 方法完全相同,但 Borg 的名称有所更改。我尝试将“for”更改为“if”,并且尝试将 vector 数组更改为 D3DXVECTOR3。我认为这是一个数组错误,但我的编程知识有限。以下是更新方法:

asteroidgamestate.h

#ifndef ASTEROIDSGAMESTATE
#define ASTEROIDSGAMESTATE

#include "Game Engine.h"
#include "Game Constants.h"
#include <vector>

class AsteroidsGameState:public GameState
{
private:
// STL vector to hold a collection of asteroid game sprites.
std::vector<GameSprite*> m_pAsteroids;
// STL vector to hold motion vectors for each asteroid.
std::vector<D3DXVECTOR3*> m_vAsteroidMotionVectors;
// STL vector to hold scaling factors for each asteroid.
std::vector<D3DXVECTOR2*> m_vAsteroidRotation;

public:
AsteroidsGameState() { }
~AsteroidsGameState()
{
this->Release();
}

//Initialises Asteroids & Borg cube
virtual bool Init()
{
D3DXVECTOR3 cSpritePosition;
GameSprite* asteroid;
D3DXVECTOR3* motionVector;
D3DXVECTOR2* rotation;
// Set up the asteroids.
for (int i = 0; i < MaximumNumberOfAsteroids / 2; i++)
{
asteroid = new GameSprite();
if (!asteroid->Init(420,425,true,L"asteroid.png"))
return false;
// Set the sprites current position.
cSpritePosition.x = (float)(100 + MathsUtilities::Get().GetRandomNumber(0, Graphics2D::Get().GetWindowWidth() - 100));
cSpritePosition.y = 1.0f;
cSpritePosition.z = 0.9f;
asteroid->SetSpritePosition(cSpritePosition);
// Set the sprites motion vector
asteroid->SetTranslationMatrix(D3DXVECTOR3(1.0f, 1.0f, 0.0f));
asteroid->SetAlive();
asteroid->SetVisible();
float scale = ((float)MathsUtilities::Get().GetRandomNumber(100, 1000)/3000.0f);
asteroid->SetScaleMatrix(scale, scale);
asteroid->SetRotationMatrix(0.0f);
this->m_pAsteroids.push_back(asteroid);
motionVector = new D3DXVECTOR3(0.0f, 0.0f, 0.0f);
motionVector->x = (float)(MathsUtilities::Get().GetRandomNumber(-1000, 1000)/150.0f) + 0.25f;
motionVector->y = (float)(MathsUtilities::Get().GetRandomNumber(-1000, 1000)/150.0f) + 0.25f;
motionVector->z = 0.0f;
this->m_vAsteroidMotionVectors.push_back(motionVector);
rotation = new D3DXVECTOR2(0.0f, 0.0f);
rotation->y = (float)(MathsUtilities::Get().GetRandomNumber(-1000, 1000)/10000.0f) + 0.001f;
this->m_vAsteroidRotation.push_back(rotation);
}


for (int i = MaximumNumberOfAsteroids / 2; i < MaximumNumberOfAsteroids; i++)
{
asteroid = new GameSprite();
if (!asteroid->Init(420,425,true,L"asteroid2.png"))
return false;
// Set the sprites current position.
cSpritePosition.x = (float)(100 + MathsUtilities::Get().GetRandomNumber(0, Graphics2D::Get().GetWindowWidth() - 100));
cSpritePosition.y = 1.0f;
cSpritePosition.z = 0.9f;
asteroid->SetSpritePosition(cSpritePosition);
// Set the sprites motion vector
asteroid->SetTranslationMatrix(D3DXVECTOR3(1.0f, 1.0f, 0.0f));
asteroid->SetAlive();
asteroid->SetVisible();
float scale = ((float)MathsUtilities::Get().GetRandomNumber(100, 1000)/3000.0f);
asteroid->SetScaleMatrix(scale, scale);
asteroid->SetRotationMatrix(0.0f);
this->m_pAsteroids.push_back(asteroid);
motionVector = new D3DXVECTOR3(0.0f, 0.0f, 0.0f);
motionVector->x = (float)(MathsUtilities::Get().GetRandomNumber(-1000, 1000)/150.0f) + 0.25f;
motionVector->y = (float)(MathsUtilities::Get().GetRandomNumber(-1000, 1000)/150.0f) + 0.25f;
motionVector->z = 0.0f;
this->m_vAsteroidMotionVectors.push_back(motionVector);
rotation = new D3DXVECTOR2(0.0f, 0.0f);
rotation->y = (float)(MathsUtilities::Get().GetRandomNumber(-1000, 1000)/10000.0f) + 0.001f;
this->m_vAsteroidRotation.push_back(rotation);
}

//Spawns one Borg
for (int i = 2 / 2; i < 2; i++)
{
asteroid = new GameSprite();
int BorgHealth = 4;
if (!asteroid->Init(420,425,true,L"borgcube.png"))
return false;
// Set the sprites current position.
/*if (BorgHealth < 4)
{
D3DXCOLOR(1.0f,1.0f,0.0f, 1.0f);
}*/
cSpritePosition.x = (float)(100 + MathsUtilities::Get().GetRandomNumber(0, Graphics2D::Get().GetWindowWidth() - 100));
cSpritePosition.y = 1.0f;
cSpritePosition.z = 0.9f;
asteroid->SetSpritePosition(cSpritePosition);
// Set the sprites motion vector
asteroid->SetTranslationMatrix(D3DXVECTOR3(1.0f, 1.0f, 0.0f));
asteroid->SetAlive();
asteroid->SetVisible();
float scale = ((float)MathsUtilities::Get().GetRandomNumber(999, 1000)/3000.0f);
asteroid->SetScaleMatrix(scale, scale);
asteroid->SetRotationMatrix(0.0f);
this->m_pAsteroids.push_back(asteroid);
motionVector = new D3DXVECTOR3(0.0f, 0.0f, 0.0f);
motionVector->x = (float)(MathsUtilities::Get().GetRandomNumber(-1000, 1000)/150.0f) + 0.25f;
motionVector->y = (float)(MathsUtilities::Get().GetRandomNumber(-1000, 1000)/150.0f) + 0.25f;
motionVector->z = 0.0f;
this->m_vAsteroidMotionVectors.push_back(motionVector);
rotation = new D3DXVECTOR2(0.0f, 0.0f);
rotation->y = (float)(MathsUtilities::Get().GetRandomNumber(-1000, 1000)/10000.0f) + 0.001f;
this->m_vAsteroidRotation.push_back(rotation);
}

return true;
}

// Update pposition, rotation of asteroids.
virtual void Update()
{
GameSprite* asteroid;
int i = 0;
std::vector<GameSprite*>::iterator it;
for (std::vector<GameSprite*>::iterator it = m_pAsteroids.begin(); it != m_pAsteroids.end(); it++)
{
asteroid = *it;
if (m_vAsteroidRotation[i]->y <= 0)
m_vAsteroidRotation[i]->x -= m_vAsteroidRotation[i]->y;
else
m_vAsteroidRotation[i]->x += m_vAsteroidRotation[i]->y;
asteroid->SetRotationMatrix(m_vAsteroidRotation[i]->x);
i++;
if (i >= MaximumNumberOfAsteroids)
i = 0;
if (asteroid->GetAlive())
{
asteroid->SetTranslationMatrix(*m_vAsteroidMotionVectors[i]);
asteroid->Update();
}
asteroid->CheckBoundary();
}
}

// At this time no action is required on entering the state.
virtual void Enter() { }

// At this time no action is required when leaving the state.
virtual void Exit() { }

// Render asteroids.
virtual void Render()
{
GameSprite* asteroid;
// Render all the asteroids.
std::vector<GameSprite*>::iterator it;
for (std::vector<GameSprite*>::iterator it = m_pAsteroids.begin(); it != m_pAsteroids.end(); it++)
{
asteroid = *it;
asteroid->Render();
}
}
// Free allocated resources.
virtual void Release()
{
// Remove Vector classes containing game objects.
this->FreeSTL(m_pAsteroids);
this->FreeSTL(m_vAsteroidMotionVectors);
this->FreeSTL(m_vAsteroidRotation);
}

// Getter functions.
// Get the STL vector to hold a collection of asteroid game sprites.
std::vector<GameSprite*> GetAsteroids() { return this-> m_pAsteroids; }
// Get the STL vector to hold motion vectors for each asteroid.
std::vector<D3DXVECTOR3*> GetAsteroidMotionVectors() { return this->m_vAsteroidMotionVectors; }
// Get the STL vector to hold scaling factors for each asteroid.
std::vector<D3DXVECTOR2*> GetAsteroidRotation() { return this->m_vAsteroidRotation; }

// Private template function to free allocatted resources.
private:
// Template methods to help destroy game objects.
template<typename T>
void FreeSTL(std::vector<T*> &list)
{
std::vector<T*>::iterator it;
it = list.begin();
while(it != list.end())
{
if ((*it) != NULL)
{
delete (*it);
it = list.erase(it);
}
else
it++;
}
list.clear();
}

// New game level requires bringing the asteroids back to life.
void NextLevelOfAsteroids()
{
GameSprite* asteroid;
D3DXVECTOR3 cSpritePosition;
float scale;
int i = 0;
std::vector<GameSprite*>::iterator it;
for (std::vector<GameSprite*>::iterator it = m_pAsteroids.begin(); it != m_pAsteroids.end(); it++)
{
asteroid = *it;
// Set the sprites current position.
cSpritePosition.x = (float)(100 + MathsUtilities::Get().GetRandomNumber(0, Graphics2D::Get().GetWindowWidth() - 100));
cSpritePosition.y = 1.0f;
cSpritePosition.z = 0.9f;
asteroid->SetSpritePosition(cSpritePosition);
// Set the sprites motion vector.
asteroid->SetTranslationMatrix(D3DXVECTOR3(1.0f, 1.0f, 0.0f));
asteroid->SetAlive();
asteroid->SetVisible();
// Scale the asteroids.
scale = ((float)MathsUtilities::Get().GetRandomNumber(100, 1000)/3000.0f);
asteroid->SetScaleMatrix(scale, scale);
asteroid->SetRotationMatrix(0.0f);
// Set motion vectors for the asteroids.
m_vAsteroidMotionVectors[i]->x = (float)(MathsUtilities::Get().GetRandomNumber(-1000, 1000)/150.0f) + 0.25f;
m_vAsteroidMotionVectors[i]->y = (float)(MathsUtilities::Get().GetRandomNumber(-1000, 1000)/150.0f) + 0.25f;
// Set up rotation vector for the asteroids.
m_vAsteroidRotation[i]->y = (float)(MathsUtilities::Get().GetRandomNumber(-1000, 1000)/10000.0f) + 0.001f;
i++;
}
}
};

#endif

borggamestate.h

#pragma once
#ifndef BORGGAMESTATE
#define BORGGAMESTATE

#include "Game Engine.h"
#include "Game Constants.h"
#include <vector>

class BorgGameState:public GameState
{
private:
// STL vector to hold a collection of borg game sprites.
std::vector<GameSprite*> m_pBorg;
// STL vector to hold motion vectors for the borg.
std::vector<D3DXVECTOR3*> m_vBorgMotionVectors;
// STL vector to hold scaling factors for the borg.
std::vector<D3DXVECTOR2*> m_vBorgRotation;

public:
BorgGameState() { }
~BorgGameState()
{
this->Release();
}

//Initialises Borg cube
virtual bool Init()
{
D3DXVECTOR3 cSpritePosition;
GameSprite* borg;
D3DXVECTOR3* motionVector;
D3DXVECTOR2* rotation;
// Set up the borg.

//Spawns one Borg
for (int i = 2 / 2; i < 2; i++)
{
borg = new GameSprite();
int BorgHealth = 4;
if (!borg->Init(420,425,true,L"borgcube.png"))
return false;
// Set the sprites current position.
/*if (BorgHealth < 4)
{
D3DXCOLOR(1.0f,1.0f,0.0f, 1.0f);
}*/
cSpritePosition.x = (float)(100 + MathsUtilities::Get().GetRandomNumber(0, Graphics2D::Get().GetWindowWidth() - 100));
cSpritePosition.y = 1.0f;
cSpritePosition.z = 0.9f;
borg->SetSpritePosition(cSpritePosition);
// Set the sprites motion vector
borg->SetTranslationMatrix(D3DXVECTOR3(1.0f, 1.0f, 0.0f));
borg->SetAlive();
borg->SetVisible();
float scale = ((float)MathsUtilities::Get().GetRandomNumber(999, 1000)/3000.0f);
borg->SetScaleMatrix(scale, scale);
borg->SetRotationMatrix(0.0f);
this->m_pBorg.push_back(borg);
motionVector = new D3DXVECTOR3(0.0f, 0.0f, 0.0f);
motionVector->x = (float)(MathsUtilities::Get().GetRandomNumber(-1000, 1000)/150.0f) + 0.25f;
motionVector->y = (float)(MathsUtilities::Get().GetRandomNumber(-1000, 1000)/150.0f) + 0.25f;
motionVector->z = 0.0f;
this->m_vBorgMotionVectors.push_back(motionVector);
rotation = new D3DXVECTOR2(0.0f, 0.0f);
rotation->y = (float)(MathsUtilities::Get().GetRandomNumber(-1000, 1000)/10000.0f) + 0.001f;
this->m_vBorgRotation.push_back(rotation);
}

return true;
}

// Update position, rotation of borg.
virtual void Update()
{
GameSprite* borg;
int i = 0;
std::vector<GameSprite*>::iterator it;
for (std::vector<GameSprite*>::iterator it = m_pBorg.begin(); it != m_pBorg.end(); it++)
{
borg = *it;
if (m_vBorgRotation[i]->y <= 0)
m_vBorgRotation[i]->x -= m_vBorgRotation[i]->y;
else
m_vBorgRotation[i]->x += m_vBorgRotation[i]->y;
borg->SetRotationMatrix(m_vBorgRotation[i]->x);
i++;
if (i >= MaximumNumberOfAsteroids)
i = 0;
if (borg->GetAlive())
{
borg->SetTranslationMatrix(*m_vBorgMotionVectors[i]);
borg->Update();
}
borg->CheckBoundary();
}
}

// At this time no action is required on entering the state.
virtual void Enter() { }

// At this time no action is required when leaving the state.
virtual void Exit() { }

// Render borg.
virtual void Render()
{
GameSprite* borg;
// Render borg.
std::vector<GameSprite*>::iterator it;
for (std::vector<GameSprite*>::iterator it = m_pBorg.begin(); it != m_pBorg.end(); it++)
{
borg = *it;
borg->Render();
}
}
// Free allocated resources.
virtual void Release()
{
// Remove Vector classes containing game objects.
this->FreeSTL(m_pBorg);
this->FreeSTL(m_vBorgMotionVectors);
this->FreeSTL(m_vBorgRotation);
}

// Getter functions.
// Get the STL vector to hold a collection of borg game sprites.
std::vector<GameSprite*> GetBorg() { return this-> m_pBorg; }
// Get the STL vector to hold motion vectors for each borg.
std::vector<D3DXVECTOR3*> GetBorgMotionVectors() { return this->m_vBorgMotionVectors; }
// Get the STL vector to hold scaling factors for the borg.
std::vector<D3DXVECTOR2*> GetBorgRotation() { return this->m_vBorgRotation; }

// Private template function to free allocatted resources.
private:
// Template methods to help destroy game objects.
template<typename T>
void FreeSTL(std::vector<T*> &list)
{
std::vector<T*>::iterator it;
it = list.begin();
while(it != list.end())
{
if ((*it) != NULL)
{
delete (*it);
it = list.erase(it);
}
else
it++;
}
list.clear();
}

// New game level requires bringing the borg back to life.
void NextLevelOfBorg()
{
GameSprite* borg;
D3DXVECTOR3 cSpritePosition;
float scale;
int i = 0;
std::vector<GameSprite*>::iterator it;
for (std::vector<GameSprite*>::iterator it = m_pBorg.begin(); it != m_pBorg.end(); it++)
{
borg = *it;
// Set the sprites current position.
cSpritePosition.x = (float)(100 + MathsUtilities::Get().GetRandomNumber(0, Graphics2D::Get().GetWindowWidth() - 100));
cSpritePosition.y = 1.0f;
cSpritePosition.z = 0.9f;
borg->SetSpritePosition(cSpritePosition);
// Set the sprites motion vector.
borg->SetTranslationMatrix(D3DXVECTOR3(1.0f, 1.0f, 0.0f));
borg->SetAlive();
borg->SetVisible();
// Scale the borg.
scale = ((float)MathsUtilities::Get().GetRandomNumber(100, 1000)/3000.0f);
borg->SetScaleMatrix(scale, scale);
borg->SetRotationMatrix(0.0f);
// Set motion vectors for the borg.
m_vBorgMotionVectors[i]->x = (float)(MathsUtilities::Get().GetRandomNumber(-1000, 1000)/150.0f) + 0.25f;
m_vBorgMotionVectors[i]->y = (float)(MathsUtilities::Get().GetRandomNumber(-1000, 1000)/150.0f) + 0.25f;
// Set up rotation vector for the borg.
m_vBorgRotation[i]->y = (float)(MathsUtilities::Get().GetRandomNumber(-1000, 1000)/10000.0f) + 0.001f;
i++;
}
}
};

结束

感谢您的帮助,如有遗漏,请见谅。

最佳答案

根据提供的信息很难说,但是您的问题可能出在这里:

在 BorgGameState.h 中更新()

        borg = *it;
if (m_vBorgRotation[i]->y <= 0)
m_vBorgRotation[i]->x -= m_vBorgRotation[i]->y;
else
m_vBorgRotation[i]->x += m_vBorgRotation[i]->y;
borg->SetRotationMatrix(m_vBorgRotation[i]->x);
i++;
if (i >= MaximumNumberOfAsteroids) // <--- should this be MaximumNumberOfBorg instead?
i = 0;

如果 m_vBorgRotation 的元素少于 MaximumNumberOfAsteroids,您将得到这样的错误。

关于c++ - vector 下标超出范围 - 更新方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21759572/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com