[英]Segmentation fault (core dumped) using async in C++11
我創建了一個將現有樹對象轉換為字符串的函數。 字符串的格式是
parent ( child1 ) ( child2 ( childOfChild2 ) )
程序正確輸出字符串,做一些其他的工作,但在和崩潰
Segmentation fault (core dumped)
這是函數( getTree(this->root)
輸出整個樹):
template <typename T>
string Tree<T>::getTree(const Node<T>& node) {
if (node.isLeaf()){
return to_string(node.value);
}
vector<future<string>> results; //each element represents a subtree connected to "node"
for (auto child:node.children){
results.push_back(move(async(&Tree<T>::getTree,this,ref(*child))));
}
string children("");
for (auto& result:results){
children+=string(" (") + result.get()+ string(") "); //this creates Segmentation fault, but the output is correct
//children+=string(""); //this does not create Segmentation fault
//children+=string("foo"); //this creates Segmentation fault
}
return to_string(node.value)+children;
}
有關變量的一些信息:
vector<shared_ptr<Node>> children;
迭代器中的比較函數不起作用 - 您還需要覆蓋兩個容器都是空的情況,即
bool operator!= (const Iter& other) const {
if (this->queueToIter.empty()&& other.queueToIter.empty()) return false;
if (this->queueToIter.empty()&&! other.queueToIter.empty()) return true;
if (!this->queueToIter.empty()&& other.queueToIter.empty()) return true;
return (this->queueToIter.front())!=(other.queueToIter.front());
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.