2013-11-27 28 views
0

对于一个学校的项目,我试图让一个二叉搜索树,我们都应该学习如何在课堂上使用“友谊”的同时。我得到在编译时的错误是:我把代码中的注释,其中的误差为清楚起见起源(请记住不允许我在BST类窝节点,他们都应该是在单独的文件和类的这种编程任务的缘故)如何检索数据从一个私人字符串

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到目前为止,我试图比较传递到与“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) 
     { 

     } 
    } 
} 

回答

0

那么......基本上你正在调用不存在的功能。当您在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); 
}; 

但更重要的是:

​​

和:

// 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在头宣布它的用法全球 - 这不是一件好事。

0

“get_key”不是BST中的一员,所以你不能仅仅把它从BST成员函数(即使它是一个友元类)。

你可以叫xPtr-> get_key(XPTR),应该工作。

但是,get_key的实施是有问题的,因为它不是真正使用本身的数据串,但所提供的XPTR参数。要纠正这个您可以:

  1. 使get_key节点的静态方法(使用节点:: get_key(XPTR)称呼它)
  2. 变化get_key返回这个 - >数据(使用xPtr-叫它> get_key());

祝你好运!

相关问题