子函数中的已赋值对象未在父函数中更新
Assigned objects in child function not updated in parent function
这是一个与法律相关的问题,但我认为这也是一个通用的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
当你完成它)
相关文章:
- 指针没有更新它在void函数内部指向的值
- (SFML)按下键时,播放器构造函数未使用正确的动画进行更新
- 尝试使用 EvtSetChannelConfigProperty() 函数更新最大事件日志文件大小时插入的错误值
- 如何更新传递到函数中的数组
- 空递归函数的更新
- 如何获取从C++发送到Lua函数的表的更新值?
- 在构造函数中更新指针向量时出现问题
- 在函数中删除第一个元素后,如何更新链接列表的phead
- 更新函数之间精灵的位置
- 为什么我的函数只在我用 & 符号传递结构时才更新结构?
- C++:使用父类运算符函数更新子类对象的继承变量
- 构造函数不更新类成员变量
- 如何每秒调用更新Qt小部件的函数
- 通过引用传递 std::string 不会更新调用函数中的字符串值
- C++ 链表 - 插入节点函数不会更新函数范围之外的值
- 如何更新函数中的C++字符串
- 更新函数在 Azure 移动库中C++损坏
- 在 C++ 中更新函数之间的变量
- 更新函数调用中的静态成员会导致崩溃
- 如何更新函数外部的数组(指向指针)