gpt4 book ai didi

c++ - C++中奇怪的段错误

转载 作者:太空狗 更新时间:2023-10-29 23:24:21 25 4
gpt4 key购买 nike

我有以下方法:

string Company::cheap(list<Candidate*>& candidates) {
candidates.sort(candidateSalaryCompare);
for (std::list<Candidate*>::iterator iter = candidates.begin(); iter
!= candidates.end(); ++iter) {
}
int m(candidates.front()->getExpectedSalary());
list<Candidate*> potentialList;
for (std::list<Candidate*>::iterator iter = candidates.begin(); (*iter)->getExpectedSalary()
== m && iter != candidates.end(); ++iter)
potentialList.push_back(*iter);
if (potentialList.size() > 0)
potentialList.sort(candidateIdCompare);
return potentialList.front()->getId();
}

按原样运行它并且我的程序可以运行,但是如果我在开始时删除空的 FOR 循环(它不执行任何操作),我会遇到段错误。有什么线索吗?

编辑

候选类,实际上我不确定我在哪一行遇到了段错误,我正在使用 eclipse,但调试器似乎不起作用

#include "../include/Candidate.h"
#include <iostream>
#include "../include/AppLogger.h"
#include <sstream>

Candidate::Candidate(string id, list<Skill> skills, list<
string> desiredJobs, double expectedSalary) :
id_(id), dateJoined_(), skills_(skills),
desiredJobs_(desiredJobs), expectedSalary_(expectedSalary),
originalSalary_(expectedSalary), gotJob_(0) {
}


void Candidate::compromise(const DateTime& currentDate) {
double salaryAfter30(0.9*this->originalSalary_);
double salaryAfter60(0.8*this->originalSalary_);
double salaryAfter90(0.7*this->originalSalary_);
Timespan duration = currentDate - this->dateJoined_;
if (duration.days() == 30 || duration.days() == 60 || duration.days() == 90) {
if (duration.days() == 30 && (this->expectedSalary_
== this->originalSalary_)) {
this->expectedSalary_ = salaryAfter30;
std::stringstream sstm;
sstm << "Candidate "<< this->getId() <<" is willing to compromise, and his expected salary is " <<this->expectedSalary_ << ".";
CAppLogger::Instance().Log(sstm.str(),
Poco::Message::PRIO_WARNING);
return;
}
else if (duration.days()==30)
poco_bugcheck_msg("Error, 30 days passed, worker already compromised");
if (duration.days() == 60 && (this->expectedSalary_ == salaryAfter30)) {
this->expectedSalary_ = salaryAfter60;
std::stringstream sstm;

sstm << "Candidate "<< this->getId() <<" is willing to compromise, and his expected salary is " <<this->expectedSalary_ << ".";
CAppLogger::Instance().Log(sstm.str(),
Poco::Message::PRIO_WARNING);
return;
}
else if (duration.days()==60)
poco_bugcheck_msg("Error, 60 days passed, worker already compromised");

if ((duration.days() == 90) && (this->expectedSalary_ == salaryAfter60)) {
this->expectedSalary_ = salaryAfter90;
std::stringstream sstm;
sstm << "Candidate "<< this->getId() <<" is willing to compromise, and his expected salary is " <<this->expectedSalary_ << ".";
CAppLogger::Instance().Log(sstm.str(),
Poco::Message::PRIO_WARNING);
return;
}
else if (duration.days()==90)
poco_bugcheck_msg("Error, 90 days passed, worker already compromised");

}
else poco_bugcheck_msg("Error, worker told to compromise when not needed");


}

list<Skill> Candidate::getSkills() const {
return this->skills_;
}

list<string> Candidate::getDesiredJobs() const {
return this->desiredJobs_;

}
double Candidate::getExpectedSalary() const {
return this->expectedSalary_;
}
DateTime Candidate::getDateJoined() const {
return this->dateJoined_;
}
DateTime Candidate::getDateLeft() const {
return this->dateLeft_;
}
void Candidate::setDateLeft(const DateTime& date) {
this->dateLeft_ = date;
}
string Candidate::getId() const {
return this->id_;
}

void Candidate::setDateJoined(const DateTime& date) {
this->dateJoined_=date;
this->setGotJob();
}

void Candidate::setGotJob() {
if (this->gotJob_==1)
std::cerr<<"error, setting gotJob while already has job"<<std::endl;
this->gotJob_=1;
}
bool Candidate::gotJob() const {
return this->gotJob_;
}
void Candidate::setQl(double ql){
jobQl_=ql;
}

int Candidate::getQl() const{
return this->jobQl_;
}

应用提供的解决方案后,出现以下错误:

assignment2(48823) malloc: *** mmap(size=140734799806464) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc

编辑

将 int m 更改为 double m,现在它似乎可以工作了因为得到预期的薪水返回是双倍的但为什么会导致该错误?

最佳答案

请看 Cris Hopman 的回答 here ,因为他的回答比我的更完整和详细,指出了可能/将导致原始代码中未定义行为(段错误)的其他问题。

这个条件是错误的:

for (std::list<Candidate*>::iterator iter = candidates.begin(); (*iter)->getExpectedSalary()
== m && iter != candidates.end(); ++iter)

您应该重新排序,以便对 end() 的测试在取消引用之前进行,并保持短路评估。事实上,您正在取消引用容器末尾的指针:

for (std::list<Candidate*>::iterator iter = candidates.begin(); 
iter != candidates.end() && (*iter)->getExpectedSalary() == m; ++iter)

关于c++ - C++中奇怪的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4430653/

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