如何从专用字符串中检索数据

How to retrieve data out of a private string

本文关键字:检索 数据 字符串 专用      更新时间:2023-10-16

对于一个学校项目,我正在尝试制作一个二进制搜索树,同时我们应该学习如何在课堂上使用"友谊"。我在编译时遇到的错误是:[为了清楚起见,我在错误来源的代码中添加了注释](请记住,我不允许在BST类中嵌套Node,为了这个编程任务,它们都应该在单独的文件和类中)

BST.cpp: In member function `void BST::insert(std::string, std::string)':
BST.cpp:11: error: `get_key' undeclared (first use this function)
BST.cpp:11: error: (Each undeclared identifier is reported only once for each function it appears in.)
BST.cpp: At global scope:
BST.cpp:5: warning: unused parameter 'data'
makefile.txt:9: recipe for target `BST.o' failed
make: *** [BST.o] Error 1

为了二进制搜索树,我希望能够访问Node.cpp中的函数,以便能够检索其私有成员。到目前为止,在BST.cpp中,我试图将传递到"insert"函数中的字符串"key"与"xTr"当前指向的字符串进行比较。类定义为:Node.h(直接位于下方)

#ifndef NODE_H_INCLUDED
#define NODE_H_INCLUDED
#include <iostream>
#include <string>
using namespace std;
class BST;
class Node
{
public:
    Node(string key, string data)
    {m_key = key; m_data = data;}
    ~Node();
    string get_key(Node *ptr); //takes in ptr to node and returns its key
    string get_data(Node *ptr); //takes in ptr to node and returns its data
    Node* get_left(Node *ptr); //takes in ptr to node and returns its left child pointer
    Node* get_right(Node *ptr); //takes in ptr to node and returns its right child pointer
private:
    string m_key;
    string m_data;
    Node *m_left;
    Node *m_right;
};

#endif // NODE_H_INCLUDED

Node.cpp

#include "Node.h"
string Node::get_key(Node* ptr)
{
    return ptr->m_key;
}
string Node::get_data(Node* ptr)
{
    return ptr->m_data;
}
Node* Node::get_left(Node* ptr)
{
    return ptr->m_left;
}
Node* Node::get_right(Node* ptr)
{
    return ptr->m_right;
}

BST.h

#ifndef BST_H_INCLUDED
#define BST_H_INCLUDED
#include <iostream>
#include <string>
using namespace std;
class BST
{
public:
    BST()
    {m_root = NULL;}
    ~BST();
    void insert(string key, string data);
    void find(string key);
    void remove(string key, string data);
    void print();
    friend class Node;
private:
    Node* m_root;
};
#endif // BST_H_INCLUDED

BST.cpp

#include "BST.h"
#include "Node.h"
void BST::insert(string key, string data)
{
    Node* yPtr = NULL;
    Node* xPtr = m_root;
    while(xPtr != NULL)
    {
        yPtr = xPtr;
        if(key < get_key(xPtr)) //error: 'get_key' undeclared (first use this function)
        {
        }
    }
}

嗯。。。基本上您调用的是不存在的函数。当您在BST中使用get_key(xPtr)时,您正在调用BSD::get_key(Node*)。你可以调用Node::get_key(Node*),但由于它不是一个静态函数(你会用Node::get_key(xPtr)调用它),你必须使用对象:

if (key < xPtr->get_key(xPtr))

如果你想像一样使用它

Node::get_key(xPtr)

您必须将Node::get_key(Node*)标记为static:

// Node.h
class BST;
class Node {
    string m_key;
    string m_data;
    Node *m_left;
    Node *m_right;
public:
    Node(string key, string data) :
        m_key(key),
        m_data(data)
        {}
    ~Node();
    static string get_key(Node *ptr);
    static string get_data(Node *ptr);
    static Node* get_left(Node *ptr);
    static Node* get_right(Node *ptr);
};

但更好的是:

// Node.h
class BST;
class Node {
    string m_key;
    string m_data;
    Node *m_left;
    Node *m_right;
public:
    Node(string key, string data) :
        m_key(key),
        m_data(data)
        {}
    ~Node();
    string get_key();
    string get_data();
    Node* get_left();
    Node* get_right();
};

和:

// BSD.cpp
string Node::get_key() {
    return m_key;
}
string Node::get_data() {
    return m_data;
}
Node* Node::get_left() {
    return m_left;
}
Node* Node::get_right() {
    return m_right;
}

然后你就可以这样使用了:

void BST::insert(string key, string data)
{
    Node* yPtr = NULL;
    Node* xPtr = m_root;
    while (xPtr != NULL) {
        yPtr = xPtr;
        if (key < xPtr->get_key()) { // actual object-oriented programming
        }
    }
}

顺便说一句:不要在头中使用using namespace std来全局声明其用法,这不是一件好事。

"get_key"不是BST的成员,因此不能仅从BST成员函数调用它(即使它是友元类)。

您可以调用xPtr->get_key(xPtr),这应该会起作用。

但是,get_key的实现是有问题的,因为它并没有真正使用自己的数据字符串,而是使用给定的xPtr参数的数据字符串。要更正此问题,您可以:

  1. 使get_key成为Node的静态方法(并使用Node::get_key(xPtr)调用它)
  2. 更改get_key以返回这个->数据(并使用xPtr->get_key()调用它)

祝你好运!