Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. "expected primary-expression before '&' token"
  2.  
  3. void print(classdatatype& x);
  4. int main()
  5. {
  6. binarySearchTree<classdatatype> tree;
  7. tree.inorderTraversal(print);
  8. return 0;
  9. }
  10.  
  11. void print(classdatatype& x) { cout << x << " "; }
  12.  
  13. template <class elemType>
  14. void binarySearchTree<elemType>::inorderTraversal(void (*visit)(elemType& item))
  15.  
  16. void bst::printfunctions(classdatatype& x)
  17. {
  18. tree.inorderTraversal(print(classdatatype & x)); //error here
  19. }
  20.  
  21. void bst::print(classdatatype& x)
  22. {
  23. cout << x << " ";
  24. }
  25.  
  26. #include <iostream>
  27. #include "windlogtype.h"
  28. using namespace std;
  29.  
  30. int main()
  31. {
  32. windlogtype wind;
  33.  
  34. ifstream infile("data.txt");
  35. //for purose of this data is one integer value
  36. infile >> wind;
  37.  
  38. //do something
  39. //main purpose is to get input
  40. return 0;
  41. }
  42.  
  43. #include "windlogtype.h"
  44. windlogtype::windlogtype() { }
  45. windlogtype::windlogtype(int i) { num = i; }
  46. int windlogtype::Getnumber() const { return num; }
  47. void windlogtype::Setnumber(int i) { num = i; }
  48. ostream operator<<(ostream& os, const windlogtype& w)
  49. {
  50. os << w.Getnumber() << 'n';
  51. return os;
  52. }
  53.  
  54. #ifndef WINDLOGTYPE_H
  55. #define WINDLOGTYPE_H
  56.  
  57. #include <iostream>
  58. using namespace std;
  59.  
  60. class windlogtype
  61. {
  62. public:
  63. windlogtype();
  64. windlogtype(int i);
  65. int Getnumber() const;
  66. void Setnumber(int i);
  67. private:
  68. int num;
  69. };
  70. ostream operator<<(ostream& os, const windlogtype& w);
  71.  
  72. #endif // WINDLOGTYPE_H
  73.  
  74. #include <iostream>
  75. #include <assert.h>
  76. using namespace std;
  77.  
  78. template <class elemType> struct binaryTreeNode
  79. {
  80. elemType info;
  81. binaryTreeNode<elemType>* llink;
  82. binaryTreeNode<elemType>* rlink;
  83. };
  84.  
  85. template <class elemType> class binarySearchTree
  86. {
  87. public:
  88. const binarySearchTree<elemType>& operator=(const binarySearchTree<elemType>&);
  89. void inorderTraversal(void (*visit) (elemType&));
  90. binarySearchTree();
  91. ~binarySearchTree();
  92. binaryTreeNode<elemType>* root;
  93. private:
  94. void inorder(binaryTreeNode<elemType>* p, void (*visit) (elemType&));
  95. };
  96.  
  97. template <class elemType> binarySearchTree<elemType>::binarySearchTree() {
  98. root = NULL;
  99. }
  100. template <class elemType> void binarySearchTree<elemType>::inorderTraversal(void (*visit) (elemType& item))
  101. {
  102. inorder(root, *visit);
  103. }
  104. template <class elemType> void binarySearchTree<elemType>::inorder(binaryTreeNode<elemType>* p, void (*visit) (elemType& item))
  105. {
  106. if (p != NULL)
  107. {
  108. inorder(p->llink, *visit);
  109. (*visit)(p->info);
  110. inorder(p->rlink, *visit);
  111. }
  112. }
  113.  
  114. #ifndef BST_H
  115. #define BST_H
  116. #include "binarySearchTree.h"
  117. #include "windlogtype.h"
  118. using namespace std;
  119.  
  120. class bst
  121. {
  122. public:
  123. bst();
  124. void InsertTree(windlogtype& newwind);
  125. void printfunctions(windlogtype& x);
  126. binarySearchTree<windlogtype>& GetTree();
  127. void print(windlogtype& x);
  128. private:
  129. binarySearchTree<windlogtype> treeRoot;
  130. };
  131. #endif // BST_H
  132.  
  133. #include "bst.h"
  134.  
  135. bst::bst(){/*ctor*/ }
  136. binarySearchTree<windlogtype>& bst::GetTree() { return treeRoot; }
  137. void bst::print(windlogtype& x) { cout << x << " "; }
  138. void bst::printfunctions(windlogtype& x)
  139. {
  140. treeRoot.inorderTraversal(print(windlogtype & x)); // error lies here
  141. }
  142.  
  143. void bst::print(classdatatype& x) // is a member function
  144.  
  145. void print(classdatatype& x); // is a free function.
  146.  
  147. template<typename Callable>
  148. void inorderTraversal(Callable visit)
  149. {
  150. inorder(root, visit); // simply pass visit further
  151. // or avoid coping by warapping std::cref(): i.e. inorder(root, std::cref(visit));
  152. }
  153.  
  154. template<typename Callable>
  155. void inorder(binaryTreeNode<elemType>* p, Callable visit)
  156. {
  157. if (p != NULL)
  158. {
  159. inorder(p->llink, visit); // or inorder(root, std::cref(visit));
  160. visit(p->info); // call directly with parameter
  161. inorder(p->rlink, visit); // or inorder(root, std::cref(visit));
  162. }
  163. }
  164.  
  165. void printfunctions(windlogtype& x)
  166. {
  167. // lambda captures the instance by copy
  168. const auto printThisLogType = [this](windlogtype & x)->void { this->print(x); };
  169. treeRoot.inorderTraversal(printThisLogType); // pass the callable lambda
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement