如何将存储患者肿瘤数据的链表传入二叉决策树处理函数?
问题:无法将链表参数传入process_data函数
我实现了基于二叉决策树的肿瘤良恶性判断系统,患者的肿瘤属性数据存储在链表中(已从CSV文件读取并填充),但调用process_data函数时无法正确将链表作为参数传入,相关代码如下:
核心问题分析
- 模板参数不匹配:
process_data函数的模板参数设计为接收逻辑类(如CancerLogic),但调用时传入了CancerLogic::patient_processing_data,且CancerLogic中结构体名为patient_processing_data而非函数中引用的processing_data,导致类型查找失败。 - 链表类非模板化:当前
PatientList未定义为模板类,无法适配patient_processing_data类型的存储,且缺少范围for循环所需的迭代器支持。 - 调用参数缺失:
cancer_tree()中调用process_data时第二个参数为空,未传入实际的链表实例。
修正方案
1. 修正ProcessPatientData.h中的函数定义
调整模板参数对应的结构体名称,适配链表节点的访问逻辑:
#pragma once #include "DecisionNode.h" #include "LinkedList.h" #include <iostream> template<typename T> void process_data(DecisionNode<typename T::patient_processing_data>* d_tree, PatientList<typename T::patient_processing_data>& patient_data) { for(auto& node : patient_data) { d_tree->process(node.data); } } template<typename T> void print_data(const PatientList<typename T::patient_processing_data>& patient_data) { for(const auto& node : patient_data) { std::cout << "Result: " << node.data.result_string << std::endl; } }
2. 将PatientList和Node改为模板类
修改链表相关类,使其支持泛型数据存储:
Node.h
#pragma once template<typename DataT> class Node { public: DataT data; Node* next; Node* previous; Node(const DataT& data_val) : data(data_val), next(nullptr), previous(nullptr) {} };
LinkedList.h
#pragma once #include "Node.h" template<typename DataT> class PatientList { private: Node<DataT>* head_; Node<DataT>* tail_; public: PatientList() : head_(nullptr), tail_(nullptr) {} void insert(const DataT& data) { Node<DataT>* new_node = new Node<DataT>(data); if(head_ == nullptr) { head_ = new_node; tail_ = new_node; return; } new_node->previous = tail_; tail_->next = new_node; tail_ = new_node; } // 实现迭代器以支持范围for循环 Node<DataT>* begin() { return head_; } Node<DataT>* end() { return nullptr; } const Node<DataT>* begin() const { return head_; } const Node<DataT>* end() const { return nullptr; } };
3. 修正cancer_tree()中的调用逻辑
传入正确的模板参数和链表实例:
#include "CancerLogic.h" #include "DecisionNode.h" #include "LinkedList.h" #include "ProcessPatientData.h" // 模拟从CSV读取数据并转换为处理用数据的函数 void load_patient_data(PatientList<CancerLogic::patient_processing_data>& list) { // 示例:生成模拟患者数据 CancerLogic::patient_processing_data data1; data1.csize_greater_than_2_r = true; list.insert(data1); // 更多患者数据填充逻辑... } void cancer_tree() { // 保留原决策树节点创建逻辑 DecisionNode<CancerLogic::patient_processing_data> malignant_node(CancerLogic::malignant); DecisionNode<CancerLogic::patient_processing_data> benign_node(CancerLogic::benign); DecisionNode<CancerLogic::patient_processing_data> ma_greater_than_3_r_node(CancerLogic::ma_greater_than_3_r, &malignant_node, &benign_node); DecisionNode<CancerLogic::patient_processing_data> ct_greater_than_5_r_node(CancerLogic::ct_greater_than_5_r, &malignant_node, &benign_node); DecisionNode<CancerLogic::patient_processing_data> ma_greater_than_5_r_node(CancerLogic::ma_greater_than_5_r, &malignant_node, &benign_node); DecisionNode<CancerLogic::patient_processing_data> csize_greater_than_3_r_node(CancerLogic::csize_greater_than_3_r, &ma_greater_than_5_r_node, &malignant_node); DecisionNode<CancerLogic::patient_processing_data> ct_greater_than_6_r_node(CancerLogic::ct_greater_than_6_r, &malignant_node, &csize_greater_than_3_r_node); DecisionNode<CancerLogic::patient_processing_data> bn_greater_than_2_r_node(CancerLogic::bn_greater_than_2_r, &ct_greater_than_6_r_node, &ma_greater_than_3_r_node); DecisionNode<CancerLogic::patient_processing_data> csize_greater_than_4_r_node(CancerLogic::csize_greater_than_4_r, &malignant_node, &bn_greater_than_2_r_node); DecisionNode<CancerLogic::patient_processing_data> cshape_greater_than_2_r_node(CancerLogic::cshape_greater_than_2_r, &csize_greater_than_4_r_node, &ct_greater_than_5_r_node); DecisionNode<CancerLogic::patient_processing_data> ma_greater_than_3_l_node(CancerLogic::ma_greater_than_3_l, &malignant_node, &benign_node); DecisionNode<CancerLogic::patient_processing_data> bc_greater_than_2_l_node(CancerLogic::bc_greater_than_2_l, &malignant_node, &ma_greater_than_3_l_node); DecisionNode<CancerLogic::patient_processing_data> ct_greater_than_3_l_node(CancerLogic::ct_greater_than_3_l, &bc_greater_than_2_l_node, &benign_node); DecisionNode<CancerLogic::patient_processing_data> bn_greater_than_3_l_node(CancerLogic::bn_greater_than_3_l, &ct_greater_than_3_l_node, &benign_node); DecisionNode<CancerLogic::patient_processing_data> csize_greater_than_2_node(CancerLogic::csize_greater_than_2, &cshape_greater_than_2_r_node, &bn_greater_than_3_l_node); // 创建并填充患者链表 PatientList<CancerLogic::patient_processing_data> patient_list; load_patient_data(patient_list); // 正确调用process_data,模板参数传入CancerLogic process_data<CancerLogic>(&csize_greater_than_2_node, patient_list); // 打印结果 print_data<CancerLogic>(patient_list); }
4. 关联原始患者数据与处理用数据
如果CSV读取的是Patients结构体数据,添加转换逻辑将原始属性映射为决策树所需的布尔标志:
// 示例转换函数 CancerLogic::patient_processing_data convert_to_processing_data(const Patients& raw_data) { CancerLogic::patient_processing_data proc_data; // 根据决策树判断条件设置对应布尔值 proc_data.ct_greater_than_5_r = (raw_data.clump_thickness > 5); proc_data.csize_greater_than_2_r = (raw_data.uniformity_of_cell_size > 2); // 其他属性同理... return proc_data; }
内容的提问来源于stack exchange,提问作者Dylan
相关产品推荐
相关产品推荐

