C++ 稀疏矩阵错误:‘SL_Node’ was not declared in this scope
C++ 稀疏矩阵错误:‘SL_Node’ was not declared in this scope
在实现 C++ 稀疏矩阵时,您可能会遇到错误消息:'SL_Node' was not declared in this scope 和 'SL_Insert' was not declared in this scope。
这些错误表明编译器无法识别 SL_Node 数据类型和 SL_Insert 函数,因为它们尚未在当前作用域中声明。
解决方法:
- 声明
SL_Node结构体: 确保在使用SL_Node之前,已经在代码中定义了它。例如:
typedef struct SL_Node {
int row;
int col;
TV value;
struct SL_Node *next;
} SL_Node;
- 定义
SL_Insert函数: 您需要定义SL_Insert函数,该函数用于将新节点插入到稀疏矩阵的链表中。
示例代码:
#include <stdlib.h> // for malloc
// ... other includes and SL_Node definition ...
void SL_Insert(SL_Node** head, SL_Node* newNode) {
// ... implementation for inserting newNode into the linked list *
}
void SM_SetAt(SparseMatrix* A, int row, int col, TV x)
{
/* 在begin和end之间添加代码 */
/***** BEGIN *****/
SL_Node* newNode = (SL_Node*)malloc(sizeof(SL_Node));
newNode->row = row;
newNode->col = col;
newNode->value = x;
newNode->next = NULL;
if (A->nodes == NULL) {
A->nodes = newNode; // 如果三元组表为空,则将新节点作为表头
}
else {
SL_Insert( &(A->nodes), newNode); // 使用 SL_Insert 函数
}
/***** END *****/
}
注意:
- 确保包含所有必要的头文件,例如
<stdlib.h>用于malloc。 - 在使用
SM_SetAt函数之前,确保SL_Node和SL_Insert已正确定义。
通过声明 SL_Node 和定义 SL_Insert,您将解决编译错误并能够成功实现 C++ 稀疏矩阵。
原文地址: https://www.cveoy.top/t/topic/cgZQ 著作权归作者所有。请勿转载和采集!