fork download
  1. #include <iostream>
  2. #include <memory>
  3. #include <vector>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. template <typename Key>
  9. class Interval
  10. {
  11. public:
  12. Key low;
  13. //Key high;//previous implementation
  14. std::vector<Key> high;//to implement... may be better to use a max priority queue
  15. //A priority queue is better since accessing a value is in o(log N) - vector is in o(N)
  16. //as well deleting and inserting is in o(log N) for priority queue
  17. //a binary tree is good as well.
  18. Key max;//to implement
  19. Key back;//represent the maximum of the list of high
  20. bool color;
  21. int N; //number of nodes under this subtree
  22. Interval *left, *right;
  23. Interval(Key lo, Key hi, Key val, bool c) : low(lo), max(val), back(val), color(c), N(1), left(NULL), right(NULL)
  24. {
  25. high.push_back(hi);
  26. }
  27. bool intersects(Key lo, Key hi) const;
  28. };
  29.  
  30.  
  31. template <typename Key>
  32. bool Interval<Key>::intersects(Key lo, Key hi) const
  33. {
  34. if (lo <= this->low && this->back <= hi) return true;
  35. if (this->low <= lo && hi <= this->back) return true;
  36. if (lo <= this->low && hi <= this->back) return true;
  37. if (this->low <= lo && this->back <= hi) return true;
  38. return false;
  39.  
  40. }
  41.  
  42. template <class Type> class Segment
  43. {
  44. private:
  45. Type x1, y1, x2, y2;
  46.  
  47. public:
  48. Segment(Type x1, Type y1, Type x2, Type y2):x1(x1), y1(y1), x2(x2), y2(y2){}
  49. inline bool isHorizontal(){return x1 == x2;}
  50. inline bool isVertical (){return y1 == y2;}
  51. //int compare(Segment segment);
  52. inline Type getx1(){return x1;}
  53. inline Type getx2(){return x2;}
  54. inline Type gety1(){return y1;}
  55. inline Type gety2(){return y2;}
  56.  
  57. };
  58.  
  59. template <typename Key>
  60. std::vector<Segment<Key> > findIntersections(const Interval<Key> &interval, Segment<Key> segment)
  61. {
  62. const Interval<Key> *x = &interval;
  63. std::vector<Segment<Key> > intersections;
  64.  
  65. while (x != NULL)
  66. {
  67. if (x->intersects(segment.gety1(), segment.gety2())) intersections.push_back(segment);
  68. else if (x->left == NULL) x = x->right;
  69. else if (x->left->max < segment.gety1()) x = x->right;//this line gives the error
  70. else if (segment.gety1() > x->left->max) x = x->right;//this line is OK
  71. else x = x->left;
  72. }
  73. return intersections;
  74. }
  75.  
  76. int main()
  77. {
  78. return 0;
  79. }
Compilation error #stdin compilation error #stdout 0s 3292KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘std::vector<Segment<Key> > findIntersections(const Interval<Key>&, Segment<Key>)’:
prog.cpp:69:41: error: ‘.’ cannot appear in a constant-expression
         else if (x->left->max < segment.gety1()) x = x->right;//this line gives the error
                                         ^
prog.cpp:69:47: error: a function call cannot appear in a constant-expression
         else if (x->left->max < segment.gety1()) x = x->right;//this line gives the error
                                               ^
prog.cpp:69:27: error: parse error in template argument list
         else if (x->left->max < segment.gety1()) x = x->right;//this line gives the error
                           ^
stdout
Standard output is empty