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

为什么进入类成员函数后成员变量指针的值丢失了?(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出来 

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

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?