【问题标题】:B+ tree insertion and searchingB+ 树插入和搜索
【发布时间】:2021-08-27 03:34:03
【问题描述】:

我正在尝试使用 b+tree 实现文件中信息的插入和搜索,同时这样做我得到一个未定义引用的错误

我已经对原始代码进行了更改,其链接在下面仅给出了 main.cpp 和头文件已更改

main.cpp

#include <iostream>
#include <vector>
//#include <algorithm>
#include <fstream>
#include <string>
//#include <filesystem>
#include "BPTree.h"

void insertionMethod(BPTree** bPTree) {
    int rollNo;
    int age, marks;
    std::string name;

    std::cout << "Please provide the rollNo: ";
    std::cin >> rollNo;

    std::cout << "\nWhat's the Name, Age and Marks acquired?: ";
   std::cin >> name >> age >> marks;

    std::string fileName = "DBFiles/";
     fileName += std::to_string(rollNo) + ".txt";
    FILE* filePtr = fopen(fileName.c_str(), "w");
    std::string userTuple = name + " " + std::to_string(age) + " " + std::to_string(marks) + "\n";
    fprintf(filePtr, userTuple.c_str());
    //fclose(filePtr);

    (*bPTree)->insert(rollNo, filePtr);
    fclose(filePtr);
    std::cout << "Insertion of roll No: " << rollNo << " Successful"<<std::endl;
}

void searchMethod(BPTree* bPTree) {
    int rollNo;
    std::cout << "What's the RollNo to Search? ";
    std::cin >> rollNo;

    bPTree->search(rollNo);
}
int main() {
  
    bool flag = true;
    int option;

    int maxChildInt = 4, maxNodeLeaf = 3;
    std::cout << "Please provide the value to limit maximum child Internal Nodes can have: ";
    std::cin >> maxChildInt;
    std::cout << "\nAnd Now Limit the value to limit maximum Nodes Leaf Nodes can have: ";
    std::cin >> maxNodeLeaf;

    BPTree* bPTree = new BPTree(maxChildInt, maxNodeLeaf);
    std::cout<<bPTree->getMaxLeafNodeLimit()<<std::endl;
    do {
        std::cout << "\nPlease provide the queries with respective keys : " << std::endl;
        std::cout << "\tPress 1: Insertion \n\tPress 2: Search<< std::endl;
        std::cin >> option;

        switch (option) {
            case 1:
                insertionMethod(&bPTree);
                break;
            case 2:
                searchMethod(bPTree);
                break;
            default:
                flag = false;
                break;
        }
    }while (flag);

    return 0;
}

BPTree.h


#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
#include <string>

#ifndef NODE_H
#define NODE_H
class Node {
   public:
    bool isLeaf;
    std::vector<int> keys;
  
    Node* ptr2next;              
    union ptr {                    
        std::vector<Node*> ptr2Tree;  //Array of pointers to Children sub-trees for intermediate Nodes
        std::vector<FILE*> dataPtr;   // Data-Pointer for the leaf node

        ptr();   // To remove the error !?
        ~ptr();  // To remove the error !?
    } ptr2TreeOrData;

    friend class BPTree;  // to access private members of the Node and hold the encapsulation concept
   public:
    Node(){
        this->isLeaf = false;
        this->ptr2next = NULL;
    }
};



class BPTree {
   private:
    int maxIntChildLimit;                                   //Limiting  #of children for internal Nodes!
    int maxLeafNodeLimit;                                   // Limiting #of nodes for leaf Nodes!!!
     Node* root;  Node* pparent = NULL;                                           //Pointer to the B+ Tree root
    void insertInternal(int x, Node** cursor, Node** child);  //Insert x from child in cursor(parent)
  Node** findParent(Node* cursor, Node* child){
       if (cursor->isLeaf || cursor->ptr2TreeOrData.ptr2Tree[0]->isLeaf)
        return NULL;

    for (int i = 0; i < cursor->ptr2TreeOrData.ptr2Tree.size(); i++) {
        if (cursor->ptr2TreeOrData.ptr2Tree[i] == child) {
            pparent = cursor;
        } else {
            Node* tmpCursor = cursor->ptr2TreeOrData.ptr2Tree[i];
            findParent(tmpCursor, child);
        }
    }

    return &pparent;
  }


   public:
    BPTree();
    BPTree(int degreeInternal, int degreeLeaf){
        this->maxIntChildLimit = degreeInternal;
    this->maxLeafNodeLimit = degreeLeaf;
    this->root = NULL;
    }

    int getMaxIntChildLimit();
    int getMaxLeafNodeLimit() 
    {
        return maxLeafNodeLimit;
    }
    void search(int key);
    void insert(int key, FILE* filePtr);
   
};
#endif

Search Insert 代码

我得到的错误是

C:\Users\admin\AppData\Local\Temp\cctqZ0YA.o:insert.cpp:(.text$_ZN4NodeC1Ev[__ZN4NodeC1Ev]+0x20): undefined reference to `Node::ptr::ptr()'
collect2.exe: error: ld returned 1 exit status

我不太确定 Node::ptr::ptr() 是什么,有人可以帮忙

【问题讨论】:

  • 如果我删除这两行我会得到一堆错误,比如use of deleted function 'Node::ptr::ptr()'

标签: c++ pointers b-tree


【解决方案1】:

删除那些已声明但未定义的构造函数和析构函数后得到的错误消息指向实际问题。为什么联合可能需要构造函数和析构函数在别处有更好的解释,例如herehere

您可以通过提供一个简单的空实现来编译您的代码。

ptr() {};
~ptr() {};

【讨论】:

  • 这行new (&amp;root-&gt;TreeOrDataPtr.DataPtr) std::vector&lt;FILE*&gt;;在搜索文件中发生了什么
猜你喜欢
  • 2012-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多