gpt4 book ai didi

c++ - 在调用接收对象拷贝的函数后,原始重置

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:17:55 26 4
gpt4 key购买 nike

我有一个函数 decidir() 接收两个对象(jogadores 和 monstros),这两个对象内部有一个指针 vector 。在我调用该函数并返回后,原始对象的 vector 各有 0 个元素。类(class):

#ifndef EQUIPE_H
#define EQUIPE_H
#include "Personagem.h"
#define MAX_INTEGRANTES 6

class Equipe
{
public:
Equipe(short novoMaximoIntegrantes = MAX_INTEGRANTES);
Equipe(Personagem *novoIntegrantes,short numIntegrantes,short novoMaximoIntegrantes = MAX_INTEGRANTES);
~Equipe();
vector<Personagem*>* getEquipe();
void adicionarIntegrante(Personagem* integrante);
void setIntegrante(Personagem* integrante, short posicao);
int getX();
void setX(int novoX);
int getY();
void setY(int novoY);
int getZ();
void setZ(int novoZ);
Personagem* removerIntegrante(short posicao = MAX_INTEGRANTES);
Personagem* operator[](short posicao);

private:
vector<Personagem*>* integrantes;
short maximoIntegrantes;
int x, y, z;
};
#endif

函数:

int* Monstro::decidir(Equipe oponentes, Equipe amigos){
int decisao[2] = { 0,0 }, nConcorrentes = 0, ataque = 0;
float rolagem = rand() % 100 + 1;
float rp = rolagem / 100;
if (rp < getPrioridadeAtaque())
ataque = 1;
if (getNumMagias()){
rolagem = abs(rolagem - 100);
rp = rolagem / 100;
for (int i = 0; i < getNumMagias(); i++){
if (getPrioriedadeMagias()[i] > rp)
nConcorrentes++;
}
if (ataque){
decisao[0] = 0;
do{
if (oponentes.getEquipe()->size()>3)
decisao[1] = rand() % 3;
else
decisao[1] = rand() % oponentes.getEquipe()->size();
if (oponentes[decisao[1]]->getHP())
break;
} while (true);
}else{
if (nConcorrentes){
rolagem = rand() % (10 * nConcorrentes) + 1;
for (int i = 1; i <= nConcorrentes; i++){
if (rolagem > (10 * (i - 1)) && rolagem < (10 * i)){
decisao[0] = i+2;
}
}
}
do{
if (getMagia(decisao[0] - 2).getEfeito() == DANO || getMagia(decisao[0] - 2).getEfeito() == DANO_STATUS){
decisao[1] = rand() % oponentes.getEquipe()->size();
if (oponentes[decisao[1]]->getHP())
break;
}
else{
decisao[1] = rand() % amigos.getEquipe()->size();
if (amigos[decisao[1]]->getHP())
break;
}
} while (true);
}
} else{
do{
if (oponentes.getEquipe()->size()>3)
decisao[1] = rand() % 3;
else
decisao[1] = rand() % oponentes.getEquipe()->size();
if (oponentes[decisao[1]]->getHP())
break;
} while (true);
}
return decisao;
}

调用函数的代码:

Equipe jogadores , monstros(30);
for(int i=0;i<6;i++){
jogadores.adicionarIntegrante(//long parameter assignment);
}
numMonstros = (rand() % 5 + 1) * jogadores.getEquipe()->size();
for (int i = 0; i < numMonstros; i++){
nomeMonstro = "Slime" + (i>0 ? to_string(i) : "");
monstros.adicionarIntegrante(new Monstro(nomeMonstro, 1, 25, 25, 0, 0, 8, 9, 10, 10, 10, 18, 1.0, 200, 10));
}
int *decisaoTemp, **decisao, combatenteAtual;
Personagem* combatente=NULL;
for (int i = 0; i < monstros.getEquipe()->size(); i++){
if (monstros[i]->getHP()){
combatente = monstros[i];
combatenteAtual = getCombatenteAtual(combatente, decisao);
decisaoTemp = combatente->decidir(jogadores, monstros);/*Before this vectors sizes
are 6 and a random number times 6*/
decisao[combatenteAtual][1] = decisaoTemp[0];//Here vector sizes are both 0
if (decisaoTemp[0] && combatente->getMagia(decisaoTemp[0]).getEfeito() == CURA){
decisao[combatenteAtual][2] = getCombatenteAlvo(combatentes, monstros, decisaoTemp[1]);
}
else{
decisao[combatenteAtual][2] = getCombatenteAlvo(combatentes, jogadores, decisaoTemp[1]);
}
}
}

最佳答案

在您的函数 decidir 中,您应该改为通过引用传递参数。意思是在对象类型之后添加 &:int* Monstro::decidir(Equipe& oponentes, Equipe& amigos) 这样当函数结束时参数对象的析构函数不会被调用。

当按值将对象传递给函数时,会创建该对象的拷贝。这意味着如果您的原始对象有一个引用地址的指针,则复制的对象也将有一个引用该地址的指针。这可能会导致问题,因为当函数结束时,复制的对象将超出范围并调用其析构函数。

关于c++ - 在调用接收对象拷贝的函数后,原始重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30795679/

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