2014-04-14 42 views
0

我在嘗試使項目的基本佈局正常工作時遇到了一些麻煩。我想從我的一個類函數調用中的另一個類調用一個函數。希望這將是更容易與這種理解:試圖在另一個類函數中使用成員類函數

Node.h:

1 #ifndef _NODE_H 
2 #define _NODE_H 
3 
4 #include<iostream> 
5 #include<string> 
6 
7 using namespace std; 
8 
9 class Node 
10 { 
11 public: 
12 Node(string *key, string *data, Node *parent); 
13 // string *get_key(); 
14 // string *get_data(); 
15 // Node *get_left(); 
16 // Node *get_right(); 
17 // Node *get_parent(); 
18 ~Node(); 
19 
20 string *m_key; 
21 string *m_data; 
22 Node *m_left; 
23 Node *m_right; 
24 Node *m_parent; 
25 }; 
26 
27 
28 #endif 

Node.cpp:

1 #include "Node.h" 
2 #include<iostream> 
3 
4 using namespace std; 
5 
6 Node::Node(string *key, string *data, Node *parent) 
7 { 
8 m_key = key; 
9 m_data = data; 
10 m_parent = parent; 
11 m_left = NULL; 
12 m_right = NULL; 
13 } 

BST.h

1 #ifndef _BST_H 
    2 #define _BST_H 
    3 
    4 #include"Node.h" 
    5 
    6 #include<iostream> 
    7 #include<string> 
    8 #include<fstream> 
    9 
    10 class BST 
    11 { 
    12 public: 
13 friend class Node; 
14 BST(); 
15 ~BST(); 
16 void insert(string key, string data); 
17 void insert(Node *root, Node *check_node); 
18 void in_order_tree_walk(Node *root); 
19 void tree_delete(Node *root, Node *cur_node); 
20 void tree_transplant(Node *root, Node *node_one, Node *node_two); 
21 Node *tree_search(Node *root, string *key); 
22 
23 private: 
24 Node *m_root; 
25 }; 
26 
27 #endif 

BST.cpp:

1 #include "Node.h" 
    2 #include "BST.h" 
    3 #include<iostream> 
    4 
    5 using namespace std; 
    6 
    7 BST::BST() 
    8 { 
    9 m_root = NULL; 
    10 } 
    11 
    12 void BST::insert(string key, string data) 
    13 { 
    14 string *my_key = new string(key); 
    15 string *my_data = new string(data); 
    16 
    17 Node *my_node = new Node(my_key, my_data, NULL); 
    18 
    19 insert(m_root, my_node); 
    20 } 
    21 
    22 void BST::insert(Node *root, Node *check_node) 
    23 { 
    24 Node *node_one = NULL; 
    25 Node *node_two = root; 
    26 
    27 while(node_two != NULL) 
    28 { 
    29 node_one = node_two; 
    30 if(*(check_node->m_key) < *(node_two->m_key)) 
    31 { 
    32 node_two = node_two->m_left; 
    33 } 
    34 else 
    35 { 
    36 node_two = node_two->m_right; 
    37 } 
    38 } 
    39 
    40 check_node->m_parent = node_one; 
    41 
    42 if(node_one == NULL) 
    43 { 
    44 root = check_node; 
    45 } 
    46 else if(*(check_node->m_key) < *(node_one->m_key))// may have to dereference 
    47 { 
    48 node_one->m_left = check_node; 
    49 } 
    50 else 
    51 { 
    52 node_one->m_right = check_node; 
    53 } 
    54 } 

BSTapp.h:

1 #ifndef _BSTAPP_H 
    2 #define _BSTAPP_H 
    3 
    4 #include"BST.h" 
    5 #include"Node.h" 
    6 
    7 #include<iostream> 
    8 #include<string> 
    9 
10 using namespace std; 
11 
12 class BSTapp 
13 { 
14 public: 
15 void insert(string key, string data); 
16 // void find(string key); 
17 // void do_delete(string key, string data); 
18 void print(); 
19 // int quit(); 
20 private: 
21 class BST; 
22 }; 
23 
24 
25 #endif 
26 
27 int main() 
28 { 
29 BSTapp myapp; 
30 
31 string command; 
32 cin >> command; 
33 
34 while(command != "quit") 
35 { 
36 if(command == "insert") 
37 { 
38 string a, b; 
39 getline(cin, a); 
40 getline(cin, b); 
41 myapp.insert(a,b); 
42 } 
43 } 
44 } 

BSTapp.cpp:

1 #include"BSTapp.h" 
2 #include"Node.h" 
3 #include"BST.h" 
4 
5 #include<iostream> 
6 #include<string> 
7 
8 using namespace std; 
9 
10 void BSTapp::insert(string key, string data) 
11 { 
12 BSTapp::BST::insert(key, data); 
13 
14 } 
15 

基本上就是我想要做的就是從BSTapp級呼叫插入,然後從BST是插入函數調用插入類。我在這個語法中遇到了很多麻煩。如果有人能指引我朝着正確的方向發展,那將會非常有幫助。我使用BSTapp作爲模板的種類,然後使用BST來執行我的樹操作

+0

你需要的是從'BSTapp'實例到'BST'實例的指針/引用。 –

回答

0

您有一個BSTapp對象(myapp),因此您可以調用myapp.insert()。 但是你沒有BST對象,所以你不能調用它的插入方法。

相關問題