nahgnez 2020-11-16 08:31 采纳率: 100%

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

``````#include<iostream>
using namespace std;
struct Triple { int row, col, value; };
class Matrix;
class MatrixNode {
friend class Matrix;
private:
MatrixNode* down;
MatrixNode* right;
union {
MatrixNode* next;
Triple triple;
};
MatrixNode(bool b, Triple* t) {
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() {
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:
};
int main() {
return 0;
}``````

• 写回答

#### 悬赏问题

• ¥20 ROS中的TEB局部规划问题
• ¥20 关于#matlab#的问题：要求测出上面图片中所有同心圆的半径
• ¥20 epanet软件运行问题
• ¥15 Python 文件读取
• ¥60 dpabi进行Alff计算时脑池有干扰信号
• ¥15 利用kmeans或kshape聚类分析对归一化的无量纲时间-降雨序列进行聚类
• ¥15 protel99.SE提示一下弹窗
• ¥15 银河麒麟v10 执行.run失败如何解决
• ¥15 如何用Python打开LA文件
• ¥15 用mysql做一个酒店管理系统