修改在其他结构(链表)中定义的结构对象的元素
modify an element of struct object defined in other struct (linked list)
struct triangle {
triangle( vertice * p0, vertice * p1, vertice * p2)
:tv1(0)
,tv2(0)
,tv3(0)
{
m_Vertices[0] = p0;
m_Vertices[1] = p1;
m_Vertices[2] = p2;
//SetCircumCircle();
}; triangle *tv1,*tv2,*tv3;}
struct vertice
{
vertice ( int init_val=0) :id (init_val) {};
vertice (double xx, double yy=0):x(xx),y(yy) { };
int id;
double x,y;
};
std:: vector <triangle> triangles;
void cre_triangle()
{
triangle r(0,1,2);
triangle r2(3,4,6);
r.tv1= &r2;
r.tv2=0;
r.tv3=0;
cout << r.tv1->m_Vertices[0]->id << r.tv1->m_Vertices[1]->id
<< r.tv1->m_Vertices[2]->id; //the result is 3 4 6
triangles.pushback(r);
}
int main ()
{
cre_triangle();
cout << triangles[0].tv1->m_Vertices[0]->id <<triangles[0].tv1->m_Vertices[1]->id
<<triangles[0].tv1->m_Vertices[2]->id << endl ;
// problem here (....exe has stopped working)
return 0;
}
我通过r(tv1是邻居1(访问了三角形r2的id和顶点坐标,并修改了它但我有问题(appilcaption.exe已停止工作(你能帮我吗。。
您正在存储指向局部变量的指针。当cre_triangle()
结束时,这些变量分解。要么使用共享指针,要么只按副本存储。
相关文章:
- 我的超类中的模板问题与结构定义
- 为私有结构定义双参数运算符重载
- 可视化C++中的结构定义错误
- 如何在结构定义中组合__attribute__((dllexport))和[[nodiscard]]?
- COM 互操作结构定义与内存布局不匹配
- C 在标题文件中创建具有结构定义的结构的队列
- 如何将结构定义为结构的成员
- 在这个节点定义中,为什么我们在结构定义之后使用"Node"?
- 不同模块中的ambigus结构定义
- 有条件的C/C 结构定义
- C++ 初始化结构定义中的向量
- 字符串声明中的结构定义中的字符串声明
- 使用结构定义函数
- 为具有许多数据成员的结构定义 == 和<
- enableif结构定义和默认模板参数
- C 使用模板为结构定义别名
- C/C++ 中的套接字编程 - 接收由 typedef 结构定义的消息
- 为C++结构定义显式析构函数如何影响调用约定
- 当结构定义在头文件中时,如何在main()中创建结构数组
- C结构定义标准