gpt4 book ai didi

c++ - 指向错误地址的指针?

转载 作者:行者123 更新时间:2023-11-30 04:10:54 24 4
gpt4 key购买 nike

我的指针有问题,它似乎指向了错误的地址。我在 League 类中有一个函数来创建时间表:

void League::CreateSchedule()
{
for (int i = 0; i < allTeamsInLeague.size(); i++)
{
int tempDay = startingDay, tempMonth = startingMonth, tempYear = Time::GetInstance().GetYear();
for (int y = 0; y < allTeamsInLeague.size(); y++)
{
if (i != y)
{
allNotPlayedLeagueMatches.push_back(NotPlayedMatch(allTeamsInLeague[i], allTeamsInLeague[y], tempDay, tempMonth, tempYear));
allTeamsInLeague[i]->GetMyMatches().push_back(&allNotPlayedLeagueMatches[allNotPlayedLeagueMatches.size() - 1]);
allTeamsInLeague[y]->GetMyMatches().push_back(&allNotPlayedLeagueMatches[allNotPlayedLeagueMatches.size() - 1]);
std::cout <<"Size of allNotPlayedLeagMatches: " << allNotPlayedLeagueMatches.size() << std::endl;
}
}
}
std::cout <<"League shedule done!" <<std::endl;
}

它将在(NotPlayedLeagueMatches 的)allNotPlayedLeagueMatches vector 中创建的比赛地址添加到类 Team 的 vector myMatches( vector (Match*),其中 Match 是纯抽象类)。

std::vector<Match*> &Team::GetMyMatches()
{
return myMatches;
}

然后,当我尝试输出球队的比赛时,它会在显示 myMatches[0] 时崩溃,但在仅显示 myMatches[1] 时不会崩溃。我尝试使用调试器解决这个问题,但它说无法读取 myMatches[0] 的内存。

这是 League.h 文件:

#pragma once
#include "PlayedMatch.h"
#include "Time.h"
#include <vector>
#include <iostream>

class League
{
public:

//Default Constructor
League();

//Overloaded Constructor
League(std::string name, int startingDay, int startingMonth);

//Destructor
~League();

std::vector<Team*> &GetAllTeamsInLeague();

void CreateSchedule();

private:

std::vector<Team*> allTeamsInLeague;

std::string name;

int startingDay, startingMonth;

std::vector<NotPlayedMatch> allNotPlayedLeagueMatches;
std::vector<PlayedMatch> allPlayedLeagueMatches;
};

联盟.cpp:

#include "League.h"


League::League()
{
name = "";
startingDay = 1;
startingMonth = 1;
}


League::~League()
{

}

League::League(std::string name, int startingDay, int startingMonth)
{
this->name = name;
this->startingDay = startingDay;
this->startingMonth = startingMonth;
}

std::vector<Team*> &League::GetAllTeamsInLeague()
{
return allTeamsInLeague;
}

void League::CreateSchedule()
{
for (int i = 0; i < allTeamsInLeague.size(); i++)
{
int tempDay = startingDay, tempMonth = startingMonth, tempYear =Time::GetInstance().GetYear();
for (int y = 0; y < allTeamsInLeague.size(); y++)
{
if (i != y)
{
allNotPlayedLeagueMatches.push_back(NotPlayedMatch(allTeamsInLeague[i], allTeamsInLeague[y], tempDay, tempMonth, tempYear));
allTeamsInLeague[i]->GetMyMatches().push_back(&allNotPlayedLeagueMatches[allNotPlayedLeagueMatches.size() - 1]);
allTeamsInLeague[y]->GetMyMatches().push_back(&allNotPlayedLeagueMatches[allNotPlayedLeagueMatches.size() - 1]);
std::cout <<"Size of allNotPlayedLeagMatches: " << allNotPlayedLeagueMatches.size() << std::endl;
}
}
}
std::cout <<"League shedule done!" <<std::endl;

}

团队.h:

#ifndef Team_H
#define Team_H

#include <iostream>
#include <string>
#include <vector>
#include "Match.h"

using namespace std;

class Team
{

public:
//Default Constructor
Team();

//Overloaded Constructor string name, int defense, int attack
Team(string, int, int, int, int);

//Destructor
~Team();

void ShowTeamCharacteristics();
//shows the team characteristics(defence, attack, name...)

int GetAttack();
//Returns team's attack ability

int GetDefense();
//Returns team's defence ability

int GetLeagueX();
//sets in which country the team is

int GetLeagueY();
//sets in which league the team is

void ShowCalendar();


string GetName();
//Returns team's name

void SetAttack(int);
//sets the new value to the current attack

void SetDefense(int);
//sets the new value to the current defense

void SetName(string);
//sets the new value to the current name

void SetLeagueX(int);
//sets in which country the team is

void SetLeagueY(int);
//sets in which league the team is

std::vector<Match*> &GetMyMatches();

private:

int defense, attack; // 100 - defense = chance to concede a goal, attack / 1.5 = chance to score a goal
string name; // The name of the team
int leagueX, leagueY;
int points;
std::vector<Match*> myMatches;


};
#endif

团队.cpp:

#include "Team.h"

Team::Team()
{
name = "";
attack = 0;
defense = 0;
leagueX = 0;
leagueY = 0;
points = 0;
}

Team::Team(string name, int defense, int attack, int leagueX, int leagueY)
{
this->name = name;
this->defense = defense;
this->attack = attack;
this->leagueX = leagueX;
this->leagueY = leagueY;
points = 0;
}


Team::~Team()
{
}

void Team::ShowCalendar()
{
for (int i = 0; i < myMatches.size(); i++)
{
myMatches[i]->ShowMatchDetails();
}
}

std::vector<Match*> &Team::GetMyMatches()
{
return myMatches;
}


void Team::ShowTeamCharacteristics()
{
system("cls");
cout <<"Name: " << name << endl <<
"Attack: " << attack << endl <<
"Defense: " << defense << endl;
}

int Team::GetAttack()
{
return attack;
}

int Team::GetDefense()
{
return defense;
}

int Team::GetLeagueX()
{
return leagueX;
}

int Team::GetLeagueY()
{
return leagueY;
}

string Team::GetName()
{
return name;
}

void Team::SetAttack(int attack)
{
this->attack = attack;
}

void Team::SetDefense(int defense)
{
this->defense = defense;
}

void Team::SetName(string name)
{
this->name = name;
}

void Team::SetLeagueX(int num)
{
leagueX = num;
}

void Team::SetLeagueY(int num)
{
leagueY = num;
}

知道哪里出了问题吗?

最佳答案

问题是当您添加额外的元素时,您的 allNotPlayedLeagueMatches vector 中的元素可能会移动。

想象一下以下场景:您将一个元素推送到一个 vector 中。为 vector 存储分配固定数量的内存,并将对象放置在那里。您现在将指针指向内存中的该位置并将其存储在某处。

现在你将额外的元素插入 vector ,直到它用完容量。因此 vector 分配了更大的内存部分,将所有元素复制到新的内存位置,然后释放原始存储空间。

问题是您的指针仍然指向旧位置并且现在悬空。只要没有人接触该内存,取消引用指针可能仍然有效,但这里仍然存在错误(取消引用悬空指针是未定义的行为)。

解决此问题的一种方法是仅在插入所有元素后才采用指向 vector 元素的指针。当然,这意味着您的 vector 必须在程序中的某个时刻“卡住”,并且不允许对其进行更多更改。如果这不是一个选项,请考虑使用在插入时不会移动对象的不同容器(如 std::list)。

关于c++ - 指向错误地址的指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20405614/

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