我有一个程序,它利用自定义BST,我需要传入一个函数指针,该指针从BST获取某些数据并将其放入vector
。
鉴于以下代码,我在第error C2065: 'result' : undeclared identifier
行
process(nodePtr->value, result);
任何想法我做错了什么?
的main.cpp
int main() {
//...
//do some things and get some data into a custom binary search tree
//...
vector<CustomStruct>* result = new vector<CustomStruct>;
void (*putValuesIntoAVectorFPtr)(CustomStruct, vector<CustomStruct>*) = &putValuesIntoAVector;
myCustomBinarySearchTree->inOrderTraversal(putValuesIntoAVectorFPtr, result);
}
void putValuesIntoAVector(CustomStruct data, vector<CustomStruct>* result) {
//do something
}
Bst.h
class Bst {
public:
void inOrderTraversal(void (*process)(T, vector<T>*), vector<T>* result);
private:
void inOrder(node<T>* nodePtr, void (*process)(T, vector<T>*), vector<T>* result);
};
template <class T>
void Bst<T>::inOrderTraversal(void (*process)(T, vector<T>* result), vector<T>* result) {
inOrder(root, process, result);
}
template <class T>
void Bst<T>::inOrder(node<T>* nodePtr, void (*process)(T, Vector<T>*), Vector<T>* result) {
if(nodePtr->left != nullptr) {
inOrder(nodePtr->left, process);
}
process(nodePtr->value, result);
if(nodePtr->right != nullptr) {
inOrder(nodePtr->right, process);
}
}
- 编辑 -
基于@WhozCraig建议的固定代码
立即工作