gpt4 book ai didi

c++ - 修复 C++ 中奇怪的写入文件错误

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

我遇到了这个非常奇怪的问题,我正在将结果写入输出文件,我使用函数 A B 和 C 我按该顺序激活它们,文件中的结果以不同的顺序打印,首先是 A,然后是从 C 开始,然后从 B 开始。我只是不明白为什么结果打印的顺序与激活顺序不同。谢谢。

代码:

void Manager::AddCommand(Command* com, ofstream &ResultFile)
{
if (com != NULL)
{
if (com->ValidCommand(ResultFile) == true)
commands.push_back(com);
else
delete com;
}
}

bool Command::ValidCommand(ofstream &Result) const
{
if(func<PrintCityCouncilList || func >HireEmployee){
Result << "Command:Failed activating function - invalid function number\n";
return false;}
if ((func == Command::PrintDepartmentEmployees) || (func == Command::PrintDepartmentExpenses) || (func == Command::PrintDepartmentStatistics)){
if(dept<Employee::Engineering ||dept>Employee::Sanitation )
{
Result << "Command:Failed activating function - invalid department number\n";
return false;
}
}
return true;
}

void Manager::ActivateCommand(Command* com, ofstream &Result)
{
if(com != NULL)
{
switch(com->GetFunction())
{
case (Command::PrintCityCouncilList): pcc->PrintCityCouncilDetails(Result);
break;
case (Command::PrintDepartmentEmployees):ActivatePrintDeprtEmployees(com->GetDepartment(), Result);
break;
case (Command::PrintEmployeeSalary):ActivateEmployeeSalary(com->GetId(), Result);
break;
case (Command::PrintDepartmentExpenses):ActivateDeprtExpenses(com->GetDepartment(), Result);
break;
case (Command::PrintCityCouncilExpenses): pcc->AllExpenses (Result);
break;
case (Command::PrintDepartmentStatistics):ActivateDeprtStatistics(com->GetDepartment(), Result);
break;
case (Command::FireEmployee): pcc->RemoveEmployeeFromCC(NULL,com->GetId(),Result);
break;
case (Command::HireEmployee): pcc->AddEmployeeToCC(com->GetId(),com->GetPrivateName(),com->GetSurName(),com->GetDate(),com->GetAddress(),com->GetDepartment(), com->GetStatus(),com->GetSalary(),com->GetPositionPercent(),com->GetPhoneNum(), Result);
break;
default:Result<<"Manager:Failed Activating command - invalid function"<<endl;
break;
}

}
}

void Manager::ActivateCommandsList(ofstream &Result)
{
Command* tmp = NULL;
if (commands.empty() == false)
{
list<Command*>::iterator iter = commands.begin();
while (iter != commands.end())
{
tmp = (Command*)(*iter);
ActivateCommand(tmp,Result);
iter++;
}
}
}

最佳答案

好的,这就是交易,如果您在 visual stdio 中运行代码,您的代码可能会遇到一些困难。你看,当它试图优化你的代码时,它有一些错误。关闭优化。同时刷新您的流,这是在将 endl 放入 cout 并将 \n 放入 printf 时完成的。还有另一种可能性,它是 Stack-Corruption 或 Heap-Corruption。检查您的动态对象是否在其边界内被引用。只要你不发密码,就像我 friend 刚才说的,我们应该咨询 Crystal 球。

关于c++ - 修复 C++ 中奇怪的写入文件错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3664296/

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