我正在尝试将infeasible
导出到文本文件,但是代码无法使用以下语法错误进行编译:
调用'(std :: list)(std :: list :: iterator&)'|
list<solution> feasible, infeasible;
for(list<solution>::iterator it = merged.begin(); it != merged.end(); it++) {
mutation(*it, input);
if(it->feasible)
feasible.push_front(*it);
else {
infeasible.push_front(*it);
ofstream outfile;
outfile.open("output.txt");
for(list<solution>::iterator it2 = infeasible.begin(); it2 != infeasible.end(); it2++)
outfile<<infeasible(*it2)<<std::endl;
}
}
答案 0 :(得分:0)
初步评论: 对于merged
中的每个项目,您编写一个具有相同名称的新文件,覆盖前一个文件。如果要避免这种情况并始终将新输出添加到同一文件的末尾,请使用:
outfile.open("output.txt", ios::app);
假设类solution
至少包含成员feasible
,正确定义了该函数mutation()
,merged
为list<solution>
1}}并且您已为solution
定义了一个插入器:
ostream& operator<< (ostream& os, const solution &s ) {
return os<<"??"; //t.b.d.
}
然后您可以稍微更改代码以使其编译:
for(auto it = merged.begin(); it != merged.end(); it++) {
mutation(*it, input);
if(it->feasible)
feasible.push_front(*it);
else {
infeasible.push_front(*it);
ofstream outfile;
outfile.open("output.txt", ios::app);
for(auto it2 = infeasible.begin(); it2 != infeasible.end(); it2++)
outfile<<*it2<<std::endl; // use directly *it2
}
}