2011-10-05 4 views
0

이것은 cgal 관련 질문이지만 일반 C++ 질문이기 때문에 여기에서 질문합니다.부모 함수에서 자식 함수의 할당 된 객체가 업데이트되지 않았습니다.

Alpha_shape_2 클래스를 사용하려고하고 이라는 서브 루틴의 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); 

을 사용하려하지만이 코드는 단순히이 오류로 인해 컴파일되지 않습니다 :

error C2513: 'CGAL::Alpha_shape_2' : no variable declared before '='

답변

1

당신은 지역 변수에 대한 포인터를 할당하고 함수를 종료 할 때 파괴됩니다.

함수에서 객체를 만들고 그 주소를 반환하려면 동적 할당을 사용해야합니다 (new 끝내면 delete를 잊지 마세요).

+0

이 부분에 대해 더 명확히 알 수 있습니까? ** 동적 할당 **을 사용해야합니까? – Graviton

+0

기록을 위해,'Alpha_shape_2 = new A (points.begin(), points.end(), FT (100000), Alpha_shape_2 :: GENERAL);를 시도했지만이 코드는 업데이트 할 수 없습니다. – Graviton

+0

@Graviton -'Alpha_shape_2 <일부 변수 이름은 여기> = new ....'- 변수에 형식을 지정하는 대신 이름을 지정해야합니다. – littleadv

관련 문제