加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在C 11中使用async进行分段故障(核心转储)

发布时间:2020-12-16 07:01:51 所属栏目:百科 来源:网络整理
导读:我创建了一个将现有树对象转换为字符串的函数.字符串的格式是 parent ( child1 ) ( child2 ( childOfChild2 ) ) 程序正确输出字符串,做一些其他的工作,但在和崩溃 Segmentation fault (core dumped) 这是函数(getTree(this- root)输出整个树): template typ
我创建了一个将现有树对象转换为字符串的函数.字符串的格式是

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;

请告诉您是否需要更多信息.完整的来源是tree.cpp和tree.h.

解决方法

迭代器中的比较函数不起作用 – 您还需要覆盖两个容器都是空的情况,即

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());
};

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读