如何从专用字符串中检索数据
How to retrieve data out of a private string
对于一个学校项目,我正在尝试制作一个二进制搜索树,同时我们应该学习如何在课堂上使用"友谊"。我在编译时遇到的错误是:[为了清楚起见,我在错误来源的代码中添加了注释](请记住,我不允许在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参数的数据字符串。要更正此问题,您可以:
- 使get_key成为Node的静态方法(并使用Node::get_key(xPtr)调用它)
- 更改get_key以返回这个->数据(并使用xPtr->get_key()调用它)
祝你好运!
相关文章:
- 通过 host() 从 af::array 检索数据会导致错误的数据
- 无法从 SQL Server 检索数据
- sd_journal_send发送二进制数据.如何使用日志检索数据?
- 在 c++ 中使用指向对象的指针检索数据
- 从新实例的向量中检索数据返回 0
- 如何通过串行终端检索数据而不破坏其在 GNU ARM 嵌入式中的基本值?
- QNETWORK停止连接并检索数据
- 如何在 c++ 中从文件中检索数据
- 如何从 linux 二进制文件中存储和检索数据
- ifstream不使用C 中的数组检索数据
- 无法从数组二次表达式中检索数据值
- 从具有多态性的类中检索数据
- 当我将字符串数组从 VBA 传递到 c++ 并将它们用作映射中的键时,为什么我无法从映射中检索数据(使用键)
- 调用readAll()后,不再无法从QIODevices检索数据.缓冲区已冲洗
- glBufferStorage + glGetBufferSubData 用于检索数据
- 如果没有数据,则创建一个包含数据的文本文件,然后读取它以检索数据
- 通过HTTP请求从站点检索数据(使用QT5.4的C++)
- 通过传递一个ID(c++)从数据库中检索数据
- 如何从专用字符串中检索数据
- 使用 QNetworkAccessManager 从 web 检索数据:文件已下载,但 QNetworkReply::r