2013-11-27 31 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());

祝你好運!

相關問題