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

为什么进入类成员函数后成员变量指针的值丢失了?(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 (标签-STM32|关键词-智能小车)
  • ¥20 关于#stm32#的问题,请各位专家解答!
  • ¥15 (标签-python)
  • ¥15 第一个已完成,求第二个做法
  • ¥20 搭建awx,试了很多版本都有错
  • ¥15 java corba的客户端该如何指定使用本地某个固定IP去连接服务端?
  • ¥15 activiti工作流问题,求解答
  • ¥15 有人写过RPA后台管理系统么?
  • ¥15 Bioage计算生物学年龄
  • ¥20 如何将FPGA Alveo U50恢复原来出厂设置哇?