首页 文章

如何从私有字符串中检索数据

提问于
浏览
0

对于一个学校项目,我试图创建一个二叉搜索树,同时我们应该学习如何在课堂上使用“友谊” . 我在编译时得到的错误是:[为了清楚起见,我在代码中放置了错误来源于错误](请记住,我不允许在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'与'xPtr'当前指向的字符串进行比较 . 这些类定义为: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)
        {

        }
    }
}

2 回答

  • 0

    嗯......基本上你是在调用不存在的函数 . 当您在BST中使用 get_key(xPtr) 时,您正在调用 BSD::get_key(Node*) . 您可以调用 Node::get_key(Node*) ,但由于 Node::get_key(xPtr) 调用 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
    
            }
        }
    }
    

    在旁注:不要在 Headers 中使用 using namespace std 来全局声明它的使用 - 这不是一件好事 .

  • 0

    “get_key”不是BST的成员,所以你不能只从BST成员函数调用它(即使它是朋友类) .

    您可以调用xPtr-> get_key(xPtr),这应该可行 .

    但是,get_key的实现是有问题的,因为它并没有真正使用自身的数据字符串,而是使用给定的xPtr参数 . 要纠正这个问题,你可以:

    • 使get_key成为Node的静态方法(并使用Node :: get_key(xPtr)调用它)

    • 更改get_key以返回this-> data(并使用xPtr-> get_key()调用它);

    祝好运!

相关问题