[英]How to use Functional Pointers in C++ for binary search Tree?
这是 BinarySearchTree.h 文件
I am creating a binary search tree template class, and want to use function pointers for the inorder traversal function, i am okay with function pointers but for some reason am lost on how to use the inorder once the code is in place. 网上没有太多其他东西对我有帮助,所以任何反馈都会很棒。
template <class T>
struct nodeType
{
// your write the code
T data;
nodeType * left;
nodeType * right;
nodeType(T key) :data(key), left(nullptr), right(nullptr) {}
};
template<class T>
typedef void (*f1Typ)(T &); // you are not restricted to a single parameter. You
// decide on how many parameters and provide rationale
template <class T>
class Bst // answer why class encapsulation is used - rationale
{
public:
// you write whatever else is needed. You already have some from Lab 9.
// insert is needed.
void inOrderTraversal(f1Typ f1 ) const;
void preOrderTraversal() const; // parameter f1 in here too. left as an exercise
void postOrderTraversal() const; // parameter f1 in here too. left as an exercise
private:
nodeType<T> *root;
// you write the rest
void inOrder(f1Typ f1, nodeType<T> *p) const;
// you write whatever else is needed.
};
// you write the rest. You already have some from Lab 9.
template<class T>
inline void Bst<T>::inorder( f1Typ f1,nodeType<T> * root) const
{
if (root->left != nullptr)
{
inorder(f1,root->left);
}
f1(root->data);
if (root->right != nullptr)
{
inorder(f1,root->right);
}
}
template<class T>
inline void Bst<T>::inorder(f1Typ f1) const
{
inorder(f1,this->root);
}
这是主要方法 // 使用 function 指针
int test1()
{
BinarySearchTree<int> intTree;
for (int i =0; i< 20; i++)
{
intTree.insertElement(i);
}
intTree.inOrderTraversal(f1t); // similar to the approach used in the text - just prints, so not very useful
return 0;
}
void f1t(int &data)
{
cout << data << " ";
}
**These are the errors**
*BinarySearchTree.h:22:1: error: template declaration of 'typedef'
22 | typedef void (*f1Typ)(T &); // you are not restricted to a single parameter. You
| ^~~~~~~
BinarySearchTree.h:71:29: error: 'f1Typ' was not declared in this scope*
这是 BinarySearchTree.h 文件
I am creating a binary search tree template class, and want to use function pointers for the inorder traversal function, i am okay with function pointers but for some reason am lost on how to use the inorder once the code is in place. 网上没有太多其他东西对我有帮助,所以任何反馈都会很棒。
template <class T>
struct nodeType
{
// your write the code
T data;
nodeType * left;
nodeType * right;
nodeType(T key) :data(key), left(nullptr), right(nullptr) {}
};
template<class T>
typedef void (*f1Typ)(T &); // you are not restricted to a single parameter. You
// decide on how many parameters and provide rationale
template <class T>
class Bst // answer why class encapsulation is used - rationale
{
public:
// you write whatever else is needed. You already have some from Lab 9.
// insert is needed.
void inOrderTraversal(f1Typ f1 ) const;
void preOrderTraversal() const; // parameter f1 in here too. left as an exercise
void postOrderTraversal() const; // parameter f1 in here too. left as an exercise
private:
nodeType<T> *root;
// you write the rest
void inOrder(f1Typ f1, nodeType<T> *p) const;
// you write whatever else is needed.
};
// you write the rest. You already have some from Lab 9.
template<class T>
inline void Bst<T>::inorder( f1Typ f1,nodeType<T> * root) const
{
if (root->left != nullptr)
{
inorder(f1,root->left);
}
f1(root->data);
if (root->right != nullptr)
{
inorder(f1,root->right);
}
}
template<class T>
inline void Bst<T>::inorder(f1Typ f1) const
{
inorder(f1,this->root);
}
这是主要方法 // 使用 function 指针
int test1()
{
BinarySearchTree<int> intTree;
for (int i =0; i< 20; i++)
{
intTree.insertElement(i);
}
intTree.inOrderTraversal(f1t); // similar to the approach used in the text - just prints, so not very useful
return 0;
}
void f1t(int &data)
{
cout << data << " ";
}
**These are the errors**
*BinarySearchTree.h:22:1: error: template declaration of 'typedef'
22 | typedef void (*f1Typ)(T &); // you are not restricted to a single parameter. You
| ^~~~~~~
BinarySearchTree.h:71:29: error: 'f1Typ' was not declared in this scope*
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.