Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "expected primary-expression before '&' token"
- void print(classdatatype& x);
- int main()
- {
- binarySearchTree<classdatatype> tree;
- tree.inorderTraversal(print);
- return 0;
- }
- void print(classdatatype& x) { cout << x << " "; }
- template <class elemType>
- void binarySearchTree<elemType>::inorderTraversal(void (*visit)(elemType& item))
- void bst::printfunctions(classdatatype& x)
- {
- tree.inorderTraversal(print(classdatatype & x)); //error here
- }
- void bst::print(classdatatype& x)
- {
- cout << x << " ";
- }
- #include <iostream>
- #include "windlogtype.h"
- using namespace std;
- int main()
- {
- windlogtype wind;
- ifstream infile("data.txt");
- //for purose of this data is one integer value
- infile >> wind;
- //do something
- //main purpose is to get input
- return 0;
- }
- #include "windlogtype.h"
- windlogtype::windlogtype() { }
- windlogtype::windlogtype(int i) { num = i; }
- int windlogtype::Getnumber() const { return num; }
- void windlogtype::Setnumber(int i) { num = i; }
- ostream operator<<(ostream& os, const windlogtype& w)
- {
- os << w.Getnumber() << 'n';
- return os;
- }
- #ifndef WINDLOGTYPE_H
- #define WINDLOGTYPE_H
- #include <iostream>
- using namespace std;
- class windlogtype
- {
- public:
- windlogtype();
- windlogtype(int i);
- int Getnumber() const;
- void Setnumber(int i);
- private:
- int num;
- };
- ostream operator<<(ostream& os, const windlogtype& w);
- #endif // WINDLOGTYPE_H
- #include <iostream>
- #include <assert.h>
- using namespace std;
- template <class elemType> struct binaryTreeNode
- {
- elemType info;
- binaryTreeNode<elemType>* llink;
- binaryTreeNode<elemType>* rlink;
- };
- template <class elemType> class binarySearchTree
- {
- public:
- const binarySearchTree<elemType>& operator=(const binarySearchTree<elemType>&);
- void inorderTraversal(void (*visit) (elemType&));
- binarySearchTree();
- ~binarySearchTree();
- binaryTreeNode<elemType>* root;
- private:
- void inorder(binaryTreeNode<elemType>* p, void (*visit) (elemType&));
- };
- template <class elemType> binarySearchTree<elemType>::binarySearchTree() {
- root = NULL;
- }
- template <class elemType> void binarySearchTree<elemType>::inorderTraversal(void (*visit) (elemType& item))
- {
- inorder(root, *visit);
- }
- template <class elemType> void binarySearchTree<elemType>::inorder(binaryTreeNode<elemType>* p, void (*visit) (elemType& item))
- {
- if (p != NULL)
- {
- inorder(p->llink, *visit);
- (*visit)(p->info);
- inorder(p->rlink, *visit);
- }
- }
- #ifndef BST_H
- #define BST_H
- #include "binarySearchTree.h"
- #include "windlogtype.h"
- using namespace std;
- class bst
- {
- public:
- bst();
- void InsertTree(windlogtype& newwind);
- void printfunctions(windlogtype& x);
- binarySearchTree<windlogtype>& GetTree();
- void print(windlogtype& x);
- private:
- binarySearchTree<windlogtype> treeRoot;
- };
- #endif // BST_H
- #include "bst.h"
- bst::bst(){/*ctor*/ }
- binarySearchTree<windlogtype>& bst::GetTree() { return treeRoot; }
- void bst::print(windlogtype& x) { cout << x << " "; }
- void bst::printfunctions(windlogtype& x)
- {
- treeRoot.inorderTraversal(print(windlogtype & x)); // error lies here
- }
- void bst::print(classdatatype& x) // is a member function
- void print(classdatatype& x); // is a free function.
- template<typename Callable>
- void inorderTraversal(Callable visit)
- {
- inorder(root, visit); // simply pass visit further
- // or avoid coping by warapping std::cref(): i.e. inorder(root, std::cref(visit));
- }
- template<typename Callable>
- void inorder(binaryTreeNode<elemType>* p, Callable visit)
- {
- if (p != NULL)
- {
- inorder(p->llink, visit); // or inorder(root, std::cref(visit));
- visit(p->info); // call directly with parameter
- inorder(p->rlink, visit); // or inorder(root, std::cref(visit));
- }
- }
- void printfunctions(windlogtype& x)
- {
- // lambda captures the instance by copy
- const auto printThisLogType = [this](windlogtype & x)->void { this->print(x); };
- treeRoot.inorderTraversal(printThisLogType); // pass the callable lambda
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement