gpt4 book ai didi

c++ - 抛出一个 std::length_error 实例,示例需要 boost

转载 作者:太空宇宙 更新时间:2023-11-04 15:38:53 25 4
gpt4 key购买 nike

这是我的第一个 c++ 项目,因此可能会有初学者失败。还是要学很多东西。

我正在通过将连接的圆柱体与点云库相匹配来为树木(真正的户外树木)建模。在我的真实场景中,我检测到大约 3000 个圆柱体,我想将其存储到具有两个级别的树(信息树)结构中。

如果两个圆柱体的终点和起点重合,我建立父<->子关系。我从根柱面开始并检测它的子柱面。对于每个 child ,这也是递归执行的。只要一个圆柱体只有一个 child ,我就将它添加到一个所谓的带有圆柱体 vector 的段中。如果 child 的 child 大小也为 1,则将孙子添加到同一段,依此类推。如果一个柱面有多个 child ,则当前段停止,并为每个 child 创建一个新段。所有段也有父子信息,因此我可以在两个方向上遍历树。

我已经设置了一个最小的运行示例来重现错误。我的测试圆柱体没有 7 个参数圆柱体,只有一个开始整数和一个结束整数。头文件和 cpp 文件:

测试圆柱体.h

#ifndef TESTCYLINDER_H
#define TESTCYLINDER_H


class TestCylinder
{
public:
int start;
int end;
TestCylinder(int a, int b);
virtual ~TestCylinder();
protected:
private:
};

#endif // TESTCYLINDER_H

测试圆柱体.cpp

#include "TestCylinder.h"

TestCylinder::TestCylinder(int a, int b)
{
start = a;
end = b;
}

TestCylinder::~TestCylinder()
{
//dtor
}

片段(它们代表两个分支部分之间的分支部分或茎部分)。父段用指针引用,子段存储在 vector 中:

测试段.h:

#ifndef TESTSEGMENT_H
#define TESTSEGMENT_H

#include "TestCylinder.h"
#include <vector>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>

class TestSegment
{
public:
TestSegment(TestCylinder cylinder, boost::shared_ptr<TestSegment> parent);
TestSegment(TestCylinder cylinder);
TestSegment();
virtual ~TestSegment();
std::vector<TestCylinder> getCylinders();
boost::shared_ptr<TestSegment> getParentSegment();
std::vector<TestSegment> getChildSegments();
bool addCylinder(TestCylinder nextCylinder);

protected:
private:
std::vector<TestCylinder> cylinders;
std::vector<TestSegment> childSegments;
boost::shared_ptr<TestSegment> parentSegment;
};

#endif // TESTSEGMENT_H

测试段.cpp:

#include "TestSegment.h"

TestSegment::TestSegment(TestCylinder cylinder)
{
cylinders.empty();
cylinders.push_back(cylinder);
}
TestSegment::TestSegment(TestCylinder cylinder, boost::shared_ptr<TestSegment> parent)
{
cylinders.empty();
cylinders.push_back(cylinder);
parentSegment = parent;
}
TestSegment::~TestSegment()
{
}

std::vector<TestCylinder>
TestSegment::getCylinders()
{
return cylinders;
}

boost::shared_ptr<TestSegment>
TestSegment::getParentSegment()
{
return parentSegment;
}

std::vector<TestSegment>
TestSegment::getChildSegments()
{
return childSegments;
}

bool TestSegment::addCylinder(TestCylinder nextCylinder)
{
TestCylinder endCylinder = cylinders.back();
int x1 = endCylinder.end;
int x2 = nextCylinder.start;
if(x1==x2)
{
cylinders.push_back(nextCylinder);
return true;
}
return false;
}

还有树类,它构建了树结构并提供了一些方法来遍历段/圆柱:

测试树.h:

#ifndef TESTTREE_H
#define TESTTREE_H
#include "TestSegment.h"

class TestTree
{
public:
TestTree(std::vector<TestCylinder> cylinders_in);
virtual ~TestTree();

/*
* Getters and Setters
*/
TestCylinder&
getRootCylinder ();
void
setRootCylinder (TestCylinder rootCylinderNew);
boost::shared_ptr<TestSegment>&
getRootSegment ();
void
setRootSegment (boost::shared_ptr<TestSegment> rootSegmentNew);


void addChildCylinders(TestCylinder& currentCylinder, TestSegment& segment);
std::vector<TestCylinder>
getChildCylinders(TestCylinder& currentCylinder);
std::vector<TestSegment>
getSegmentList ();
std::vector<TestCylinder>
getCylinderList ();

protected:
private:
std::vector<TestCylinder> cylinders ;
boost::shared_ptr<TestSegment> rootSegment;
std::vector<TestSegment> getChildSegmentsRecursively(TestSegment parent);
};

#endif // TESTTREE_H

测试树.cpp

#include "TestTree.h"


TestTree::TestTree(std::vector<TestCylinder> cylinders_in)
{
cylinders = cylinders_in;
TestCylinder rootCylinder = cylinders.at(0);
TestSegment root(rootCylinder);
rootSegment = boost::make_shared<TestSegment>(root);
addChildCylinders(rootCylinder,*rootSegment);
}

TestTree::~TestTree()
{
//dtor
}


std::vector<TestCylinder>
TestTree::getCylinderList ()
{

std::vector<TestCylinder> cylinders;
std::cout << "GetCylinderList before getSegmentsList" << "\n";
std::vector<TestSegment> allSegments = getSegmentList();
std::cout << "GetCylinderList after getSegmentsList" << allSegments.size()<<"\n";
for(std::vector<TestSegment>::iterator it = allSegments.begin(); it!= allSegments.end(); ++it)
{
TestSegment segment = *it;
//std::cout << segment.getSegmentCylinders().size();
cylinders.insert(cylinders.end(),segment.getCylinders().begin(), segment.getCylinders().end());
}
return cylinders;
}

std::vector<TestSegment>
TestTree::getSegmentList ()
{
// std::cout<<"GetSegmentList rootsegment size :"<< rootSegment->get.size()<<"\n";
return getChildSegmentsRecursively(*rootSegment);
}

std::vector<TestSegment>
TestTree::getChildSegmentsRecursively(TestSegment parent)
{

std::vector<TestSegment> allSegments;
allSegments.push_back(parent);
for(std::vector<TestSegment>::iterator it = parent.getChildSegments().begin (); it != parent.getChildSegments().end (); ++it)
{
TestSegment child = *it;
std::vector<TestSegment> children = getChildSegmentsRecursively(child);
allSegments.insert(allSegments.end(),children.begin(),children.end());
}
//std::cout << "TestTree geChildSegmentsRecursively" << parent.getChildSegments().size()<< "\n";
return allSegments;
}


void TestTree::addChildCylinders(TestCylinder& currentCylinder, TestSegment& segment)
{
std::vector<TestCylinder> children = getChildCylinders(currentCylinder);
if(children.size()==1)
{
TestCylinder child = children.at(0);
if(segment.addCylinder(child))
{
addChildCylinders(child,segment);
}
}
else if (children.size()>1)
{
for (std::vector<TestCylinder>::iterator it = children.begin (); it != children.end (); ++it)
{
TestCylinder child = *it;
boost::shared_ptr<TestSegment> seg_ptr = boost::make_shared<TestSegment>(segment);
TestSegment childSegment (child, seg_ptr);
if (childSegment.getParentSegment() == rootSegment)
{
std::cout << "TestTreeCpp::addChildCylinders, rootSegment is parentSegment \n";
}
addChildCylinders(child,childSegment);
}
}
}


std::vector<TestCylinder>
TestTree::getChildCylinders(TestCylinder& currentCylinder)
{
std::vector<TestCylinder> children;

for (std::vector<TestCylinder>::iterator it = cylinders.begin (); it != cylinders.end (); ++it)
{
TestCylinder cylinder = *it;
if(currentCylinder.end == cylinder.start)
{
children.push_back(cylinder);
}
}
return children;
}


/*
* Getters and Setters
*/



TestCylinder&
TestTree::getRootCylinder ()
{
TestSegment root = *rootSegment;
return root.getCylinders().at(0);
}


boost::shared_ptr<TestSegment>&
TestTree::getRootSegment ()
{
return rootSegment;
}

//void
//TestTree::setRootSegment (SegmentModel rootSegmentNew)
//{
// rootSegment = rootSegmentNew;
//}

主要功能,仅创建 6 个连接的圆柱体并使用此列表调用树:

int
main (int argc,
char** argv)
{

// ------------------------------------
// -----Load Cherry 1 -----
// ------------------------------------
std::vector<TestCylinder> cylinders;
TestCylinder cyl(0,1);
cylinders.push_back(cyl);
TestCylinder cyl2(1,2);
cylinders.push_back(cyl2);
TestCylinder cyl3(2,3);
cylinders.push_back(cyl3);
TestCylinder cyl4(3,4);
cylinders.push_back(cyl4);
TestCylinder cyl5(3,14);
cylinders.push_back(cyl5);
TestCylinder cyl6(14,15);
cylinders.push_back(cyl6);

std::cout << cylinders.size() << "\n";
TestTree tree (cylinders);
std::cout << tree.getCylinderList().size() << "\n";
}

建树可能可行,调用getCylinderList函数时出现如下错误:

terminate called after throwing an instance of 'std::length_error'
what(): vector::_M_range_insert
Aborted (core dumped)

Possible dublicate (对我而言)没有帮助,因为我在迭代时不添加或删除项目。

最佳答案

问题是 TestSegment 按值返回 vector ,例如在 TestSegment::getChildSegments 中。这意味着,您制作了 vector 的拷贝。

现在,在 getCylinderlist 中,您使用参数 segment.getCylinders().begin() 和 segment.getCylinders( )。结尾()。但是这两个对 getCylinders 的调用不会返回相同的 vector ,而是返回每个 vector 的单独拷贝。所以你实际上是在说

std::vector< TestCylinder > c1 = segment.getCylinders();
std::vector< TestCylinder > c2 = segment.getCylinders();
cylinders.insert( cylinders.end(), c1.begin(), c2.end() )

c1 和 c2 不一样,这就是代码失败的原因。

同样的问题出现在 TestSegment::getChildSegments 上。

要解决这个问题,您应该通过 const& 返回 vector 。

希望对您有所帮助。

关于c++ - 抛出一个 std::length_error 实例,示例需要 boost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26998004/

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