试图实现通过引用传递给二叉树使用的向量,我错过了什么?
问题描述:
我正在尝试编写代码以将二进制树的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);
}
}
答
声明参数作为通过参考传递,例如
void vectorExport(TreeNode *, vector<double>&);
不
void vectorExport(TreeNode *, vector<double>);
谢谢你,我知道我错过了什么 – dwagner6