繁体   English   中英

子功能中的分配对象未在父功能中更新

[英]Assigned objects in child function not updated in parent function

这是一个与cgal相关的问题,但我认为它也是一个通用的C ++问题,因此我在这里提出。

我正在尝试使用Alpha_shape_2类,并将其分配给名为GetAlphaShalCg的子例程中的AlphaShapeCg类。 问题在于Alpha_shape_2中的某些函数未返回正确的结果。

这是我的代码,非常简单,但是我不太清楚为什么将Alpha_shape_2分配给子例程中的包装器,然后在父例程中访问成员与直接访问Alpha_shape_2

如果安装了CGAL ,这是可以编译和使用的完整代码。

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/algorithm.h>
#include <CGAL/Delaunay_triangulation_2.h>
#include <CGAL/Alpha_shape_2.h>

#include <iostream>
#include <fstream>
#include <vector>
#include <list>


typedef CGAL::Exact_predicates_inexact_constructions_kernel K;

typedef K::FT FT;

typedef K::Point_2  Point;
typedef K::Segment_2  Segment;


typedef CGAL::Alpha_shape_vertex_base_2<K> Vb;
typedef CGAL::Alpha_shape_face_base_2<K>  Fb;
typedef CGAL::Triangulation_data_structure_2<Vb,Fb> Tds;
typedef CGAL::Delaunay_triangulation_2<K,Tds> Triangulation_2;

typedef CGAL::Alpha_shape_2<Triangulation_2>  Alpha_shape_2;


template <class OutputIterator>
bool
file_input(OutputIterator out)
{
  std::ifstream is("./data/fin", std::ios::in);

  if(is.fail()){
    std::cerr << "unable to open file for input" << std::endl;
    return false;
  }

  int n;
  is >> n;
  std::cout << "Reading " << n << " points from file" << std::endl;
  CGAL::copy_n(std::istream_iterator<Point>(is), n, out);

  return true;
}

//------------------ main -------------------------------------------


struct AlphaShapeCg
{

    Alpha_shape_2 *AlphaShape;
};

void GetAlphaShalCg(AlphaShapeCg *ashape,  std::list<Point> points)
{

      Alpha_shape_2 A(points.begin(), points.end(),
          FT(100000),
          Alpha_shape_2::GENERAL);
    ashape->AlphaShape=&A;
}



int main()
{
  std::list<Point> points;
  if(! file_input(std::back_inserter(points))){
    return -1;
  }

   AlphaShapeCg ashape;


   GetAlphaShalCg(&ashape, points);

   Alpha_shape_2 *APtrs=(ashape.AlphaShape);
   int alphaEigenValue = APtrs->number_of_alphas(); // gives incorrect result; alphaEigenValue=0

  //Alpha_shape_2 A(points.begin(), points.end(),
  //  FT(100000),
  //  Alpha_shape_2::GENERAL);
  //   int alphaEigenValue = APtrs->number_of_alphas(); // gives correct result; alphaEigenValue!=0

}

更新:我尝试使用

Alpha_shape_2 =new A(points.begin(), points.end(), FT(100000), Alpha_shape_2::GENERAL);

但是由于以下错误,该代码根本无法编译:

错误C2513:“ CGAL :: Alpha_shape_2”:在“ =”之前未声明任何变量

您正在分配一个指向本地变量的指针,该指针在退出函数时会被销毁。

如果要在函数中创建对象并返回其地址,则应使用动态分配( new分配的对象,完成后别忘了delete )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM