所以我一直在尝试文件输入和输出来保存Yugioh的名单,我在大多数情况下都能使用它。我使用deques来存储一个名为Card的类对象,其中包含所有信息,还有一个Deck类来为每种类型的deck,main,side和extra保存Card对象的deques。
所以无论如何,关于这个问题。我使用本地双端队列来保存以前加载的卡片的信息,因为当我加载每张卡片时,我搜索deque以获得完全匹配,然后将数字存储在变量中以用于输出。当我尝试添加到这个称为“已处理”的本地双端队列时,我收到错误。
调试断言失败!表达式:deque迭代器不可解除引用
void Deck::SaveDeck(std::string filename)
{
std::ofstream outFile;
outFile.open(filename.c_str());
//send the file the deck name
outFile << this->deckName << "\n";
//send the deck sizes
outFile << main_deck_size << " " << side_deck_size << " " << extra_deck_size << "\n";
//iterator to iterate through the entire deck looking for copies
std::deque<Card>::iterator iter = main_deckCards.begin();
std::deque<Card>::iterator iter2 = main_deckCards.begin();
//deque to hold previously added cards
std::deque<Card> processed;
std::deque<Card>::iterator processed_iter = processed.begin();
string setNAME = "";
int setNum = 0;
int quantity = 0;
bool is_processed = false;
for (int i = 0; i < main_deck_size; i++)
{
//reset processed flag
is_processed = false;
//check processed queue for exact card
for (int j = 0; j < processed.size(); j++)
{
if (iter[i].SET_NAME == processed_iter[j].SET_NAME && iter[i].SET_NUM == processed_iter[j].SET_NUM)
{
is_processed = true;
break;
}
}
if(is_processed == false)
{
//reset variables
setNAME = "";
setNum = 0;
quantity = 0;
//draw from the next card
setNAME = iter[i].SET_NAME;
setNum = iter[i].SET_NUM;
quantity++;
//loop to look for similar cards
for (int x = i+1; x < main_deck_size; x++)
{
if (iter2[x].SET_NAME == setNAME && iter2[x].SET_NUM == setNum)
{
quantity++;
}
}
outFile << setNAME << " " << setNum << " " << quantity << "\n";
if(setNAME == "LOB-EN")
{
//removing this line causes the program to work
processed.push_back(LOB[setNum]);
}
}
}
}
删除我试图将对象放入双端队列的行,程序运行没有错误,除了它多次写入相同的卡片,我想处理每个卡片的数量而不是倍数的文件出现在不同的路线上。我已经检查了其他问题,问题是由于尝试在空双端队列中使用std :: deque :: pop_front / back。但我根本不打电话给pop_front /。
关于可能导致错误的任何想法?
答案 0 :(得分:1)
使用processed.push_back(LOB[setNum]);
可能会使processed_iter
无效。
在循环的第一轮中,当processed
开始为空时,失效或多或少得到保证。