0
我正在嘗試編寫代碼以將二進制樹的inorder內容卸載到向量中。 IE:試圖實現通過引用傳遞給二叉樹使用的向量,我錯過了什麼?
#include <iostream>
#include <vector>
#include "BinaryTree.h"
using namespace std;
int main()
{
BinaryTree tree;
vector <double> v;
// Test iterative insert
cout << "Inserting the numbers 5 8 3 12 9.";
tree.insert(5);
tree.insert(8);
tree.insert(3);
tree.insert(12);
tree.insert(9);
// Test vectorExport()
tree.vectorExport(v);
}
我得到了一大堆錯誤的,因爲我不認爲我正確地實現它的成員函數做。我不知道我是否在錯誤的地方使用了&符號,或者它不在應該的位置。任何人的幫助,將不勝感激。
這是我收到的錯誤: [Error] prototype for 'void BinaryTree::vectorExport(BinaryTree::TreeNode*, std::vector<double>&)' does not match any in class 'BinaryTree'
這裏是我的類:
#ifndef DOUBLEBINARYTREE_H
#define DOUBLEBINARYTREE_H
#include <iostream>
#include <vector>
using namespace std;
class BinaryTree
{
private:
// The TreeNode class is used to build the tree.
class TreeNode
{
friend class BinaryTree;
double value;
TreeNode *left;
TreeNode *right;
TreeNode(double value1, TreeNode *left1 = NULL,
TreeNode *right1 = NULL)
{
value = value1;
left = left1;
right = right1;
}
};
TreeNode *root; // Pointer to the root of the tree
// Various helper member functions.
void insert(TreeNode *&, double);
bool search(TreeNode *, double);
void destroySubtree(TreeNode *);
void remove(TreeNode *&, double);
void makeDeletion(TreeNode *&);
void vectorExport(TreeNode *, vector<double>);
public:
// These member functions are the public interface.
BinaryTree() // Constructor
{ root = NULL; }
~BinaryTree() // Destructor
{ destroySubtree(root); }
void insert(double num)
{ insert(root, num); }
bool search(double num)
{ search(root, num); }
void remove(double num)
{ remove(root, num);}
void vectorExport(vector<double> & v)
{ vectorExport(root, v); }
};
下面是實際的功能:
//*********************************************************
// This function offloads tree contents to a vector *
//*********************************************************
void BinaryTree::vectorExport(TreeNode *tree, vector<double> &v)
{
if (tree)
{
vectorExport(tree->left, vector<double> v);
v.push_back(tree->value);
vectorExport(tree->right, vector <double> v);
}
}
謝謝你,我知道我錯過了什麼 – dwagner6