nahgnez 2020-11-16 08:31 采纳率: 100%
浏览 72
已采纳

为什么进入类成员函数后成员变量指针的值丢失了?(C++、链表实现稀疏矩阵)

#include<iostream>
using namespace std;
struct Triple { int row, col, value; };
class Matrix;
class MatrixNode {
	friend class Matrix;
private:
	MatrixNode* down;
	MatrixNode* right;
	bool head;
	union {
		MatrixNode* next;
		Triple triple;
	};
	MatrixNode(bool b, Triple* t) {
		head = b;
		if (b) { right = down = this; }
		else triple = *t;
	}
};
class Matrix {
public:
	Matrix() {
		Triple t1 = { 0,0,2 }, t2 = { 1,0,4 }, t3 = { 1,3,3 }, t4 = { 3,0,8 }, t5 = { 3,3,1 }, t6 = { 4,2,6 };
		headNode = new MatrixNode(true, 0);
		MatrixNode h0(true, 0), h1(true, 0), h2(true, 0), h3(true, 0), h4(true, 0);
		headNode->next = &h0, h0.next = &h1, h1.next = &h2, h2.next = &h3, h3.next = &h4, h4.next = headNode;
		MatrixNode n00(false, &t1), n10(false, &t2), n13(false, &t3), n30(false, &t4), n33(false, &t5), n42(false, &t6);
		h0.right = &n00, n00.right = &h0;
		h1.right = &n10, n10.right = &n13, n13.right = &h1;
		h3.right = &n30, n30.right = &n33, n33.right = &h3;
		h4.right = &n42, n42.right = &h4;
		h0.down = &n00, n00.down = &n10, n10.down = &n30, n30.down = &h0;
		h2.down = &n42, n42.down = &h2;
		h3.down = &n13, n13.down = &n33, n33.down = &h3;
	}
	~Matrix() { delete headNode; }
	Matrix operator+(const Matrix& b) const {

	}
	Matrix operator*(const Matrix& b) const {

	}
	void output() {
		MatrixNode* h = headNode->next;    //此处开始,除headNode,其后的链接全部丢失了...奇怪的是回到此时回到main函数栈中看到main函数里的m1的headNode之后的链接也丢失了...
		while (h != headNode) {
			MatrixNode* n = h->right;
			while (n != h) {
				cout << "Row: " << n->triple.row << " Col: " << n->triple.col << " Value: " << n->triple.value << endl;
				n = n->right;
			}
			h = h->next;
		}
	}
private:
	MatrixNode* headNode;
};
int main() {
	Matrix m1;    //在此处打断点,看到m1有正确初始化,headNode->next->right能看到结点
	m1.output();    //进入output函数之后,只剩headNode指针正确,其next指针变为0xcccccccc,之后的结点也不复存在
	return 0;
}

问题写在注释里了,不知道是什么原因:/希望大佬能够解答!谢谢

  • 写回答

1条回答 默认 最新

  • carrottttt 2020-11-16 09:22
    关注
    headNode = new MatrixNode(true, 0);
    		MatrixNode * h0 = new MatrixNode(true, 0);
    		MatrixNode * h1 = new MatrixNode(true, 0);
    		MatrixNode *h2 = new MatrixNode(true, 0);
    		MatrixNode *h3 = new MatrixNode(true, 0);
    		MatrixNode *h4 = new MatrixNode(true, 0);
    		headNode->next = h0, h0->next = h1, h1->next = h2, h2->next = h3, h3->next = h4, h4->next = headNode;
    		MatrixNode *n00 = new MatrixNode(false, &t1);
    		MatrixNode *n10 = new MatrixNode(false, &t2);
    		MatrixNode *n13 = new MatrixNode(false, &t3);
    		MatrixNode *n30 = new MatrixNode(false, &t4);
    		MatrixNode *n33 = new MatrixNode(false, &t5);
    		MatrixNode *n42 = new MatrixNode(false, &t6);

     

    要NEW出来 

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 51单片机使lcd显示器开机闪烁预设字符闪烁3次需要加什么代码
  • ¥50 C# 使用DEVMOD设置打印机首选项
  • ¥15 麒麟V10 arm安装gdal
  • ¥15 想用@vueuse 把项目动态改成深色主题,localStorge里面的vueuse-color-scheme一开始就给我改成了dark,不知道什么原因(相关搜索:背景颜色)
  • ¥20 OPENVPN连接问题
  • ¥15 flask实现搜索框访问数据库
  • ¥15 mrk3399刷完安卓11后投屏调试只能显示一个设备
  • ¥100 如何用js写一个游戏云存档
  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题