尝试实现通过引用传递的向量以与二叉树一起使用,我错过了什么



我正在尝试编写代码以将二叉树的无序内容卸载到向量中。即:

#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);
    }
}

将参数声明为通过引用传递,例如

void vectorExport(TreeNode *, vector<double>&);

void vectorExport(TreeNode *, vector<double>);

最新更新