问题是另一个访问 vector 并删除迭代器的类的愚蠢错误。与下面的代码无关。抱歉浪费您的时间。
我一定是漏掉了一些基本的东西。 我有一个函数可以创建一个对象,操作它的数据,然后将它插入一个 vector 中。 函数退出的那一刻,程序崩溃并出现 SIGSEV,我只能盯着 (Kdevelop gcc 4.5 gdb) 看:
/**
* The dtor only erases the elements, and note that if the
* elements themselves are pointers, the pointed-to memory is
* not touched in any way. Managing the pointer is the user's
* responsibility.
*/
~vector()
{ std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
_M_get_Tp_allocator()); }
我不是在存储指针,我是在尝试存储实例化对象。
void Init::initIndividual(int ID, int gen)
{
Individual temp_person = Individual(ID,gen);
int inst_size = getRandom<int>(1,max_inst_size);
for (int k=0;k<inst_size;k++)
{
retry:
// (1) randomly choose a body part
int body_num = getRandom<int>(1,20);
body_part temp_part = get_body_part(body_num);
// NOTE: We need to make sure that the body part is unique!
std::vector<Instruction> already_existing = temp_person.get_instructions();
if (already_existing.size() > 0)
{
for (int a=0; a< already_existing.size();a++)
{
std::string name = already_existing[a].get_body_part();
if ( name.compare(temp_part.name) == 0 )
{ //if body part already exists in the list, retry!
goto retry;
}
}
}
// (2) Create a new Instruction for this body part
Instruction temp_inst = Instruction(temp_part.name,temp_part.max_angle,temp_part.min_angle);
// (3) Randomly pick a number of body parameters to use
int paramsize = getRandom<int>(1,max_params_size);
// (4) Randomly choose time and degree trajectory parameters for this body part and append!
for (int x=0;x < paramsize; x++)
{
float time = 0.0f;
int choice = 0;
// (4.a) If begin of body parameters
if (x==0)
{
//if always start at time = 0
if (static_time_init)
{
time = 0.0f;
}
//if randomly choose the init time
else if (!static_time_init)
{
time = getRandom<float>(0.0f,(float)(time_constrain-1));
}
}
// (4.b) if not @ start of params
else if(x!=0)
{
redo:
float previous_time = temp_inst.parameters.back().time; //get previous time
double incrementor = getRandom<double>(0.1,1.0); //increment time by min 0.1 max 1.0
time = previous_time + (float)incrementor;
if (time > time_constrain) //if current time is more than time constrain, redo
{
goto redo;
}
}
// (5) Randomly pick a degree to move to (within body part constrains)
float degree = getRandom<float>(temp_inst.get_min_angle(),temp_inst.get_max_angle());
Parameter foo = Parameter(time,degree);
temp_inst.add_parameter(Parameter(time,degree));
}
temp_person.add_Instruction(temp_inst);
}
temp_person.endtime = time_constrain;
}
这就是整个函数。
std::vector<Individual> population;
推回对象时,push_back 函数不复制对象吗? 调用析构函数是因为 push_back 试图销毁 temp_person 吗? 我没有在 Individual 类中定义复制运算符。 我以前遇到过这个问题,但一直没有弄明白。 发生这种情况是因为在函数末尾 temp_person 超出范围吗? 谢谢!
编辑:类(class)个人
class Individual
{
friend class Population;
friend class Crossover;
friend class Init;
private:
std::string xml_file;
char *arg4;
protected:
bool saved, mutated, dead;
unsigned int UID, generation;
int executions;
std::vector<Instruction> instructions;
int father_UID, mother_UID;
double eta,endtime;
public:
int uniform;
float fitness;
pthread_mutex_t thread_mutex;
//Some other functions irrelevant
请注意,指令 vector 有另一个结构 vector 。
class Instruction
{
friend class Crossover;
private:
unsigned int param_size;
float max_angle, min_angle;
bool micro_mutated;
public:
std::string body_part;
std::vector<Parameter> parameters;
//other stuff
class Parameter
{
public:
float time;
float degree;
Parameter(float t,float d);
};
这里没有什么疯狂的。
这可能是 population.push_back 获得的浅拷贝的问题吗?
最佳答案
pthread_mutex_t thread_mutex;
复制 pthread_mutex_t 没有意义。我敢打赌这是问题的一部分。我也怀疑你的 char*;谁拥有它?
关于为什么我相信 pthread_mutex_t 无法复制的理由:获得初始化互斥锁的唯一记录方式是使用 pthread_mutex_init。此外,所有 pthread_mutex_* 函数都使用传递指针来操作互斥体(与 pthread_thread_t 不同)。
关于对象被推回并且函数退出后,C++ vector 推回崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6471680/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信
当我在Rails控制台中按向上或向左箭头时,出现此错误:irb(main):001:0>/Users/me/.rvm/gems/ruby-2.0.0-p247/gems/rb-readline-0.4.2/lib/rbreadline.rb:4269:in`blockin_rl_dispatch_subseq':invalidbytesequenceinUTF-8(ArgumentError)我使用rvm来管理我的ruby安装。我正在使用=>ruby-2.0.0-p247[x86_64]我使用bundle来管理我的gem,并且我有rb-readline(0.4.2)(人们推荐的最少
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
我在Rails工作并有以下类(class):classPlayer当我运行时bundleexecrailsconsole然后尝试:a=Player.new("me",5.0,"UCLA")我回来了:=>#我不知道为什么Player对象不会在这里初始化。关于可能导致此问题的操作/解释的任何建议?谢谢,马里奥格 最佳答案 havenoideawhythePlayerobjectwouldn'tbeinitializedhere它没有初始化很简单,因为你还没有初始化它!您已经覆盖了ActiveRecord::Base初始化方法,但您没有调