fork download
  1. #ifndef VECTOR2_H
  2. #define VECTOR2_H
  3.  
  4. #include <string>
  5.  
  6. template<typename T>
  7. struct Vector2
  8. {
  9. T x, y;
  10.  
  11. template<typename T>
  12. Vector2(T x = 0.0, T y = 0.0) :
  13. x(x),
  14. y(y)
  15. {
  16.  
  17. }
  18.  
  19. template<typename T>
  20. std::string toString() const
  21. {
  22. char buffer[255];
  23. snprintf(buffer, 255, "Vector2[%+.2f %+.2f]", x, y);
  24. return std::string(buffer);
  25. }
  26.  
  27. template<typename T>
  28. T operator[](int i) const
  29. {
  30. return (&x)[i];
  31. }
  32.  
  33. template<typename T>
  34. T & operator[](int i)
  35. {
  36. return (&x)[i];
  37. }
  38.  
  39. template<typename T>
  40. bool operator==(const Vector2<T> & other) const
  41. {
  42. return (&*this == &other) || (x == other.x && y == other.y);
  43. }
  44.  
  45. template<typename T>
  46. bool operator!=(const Vector2<T> & other) const
  47. {
  48. return !(*this == other);
  49. }
  50.  
  51. template<typename T>
  52. Vector2 operator+=(const Vector2<T> & other)
  53. {
  54. x += other.x;
  55. y += other.y;
  56. return *this;
  57. }
  58.  
  59. template<typename T>
  60. Vector2 operator+=(const T f)
  61. {
  62. x += f;
  63. y += f;
  64. return *this;
  65. }
  66.  
  67. template<typename T>
  68. Vector2 operator-=(const Vector2<T> & other)
  69. {
  70. x -= other.x;
  71. y -= other.y;
  72. return *this;
  73. }
  74.  
  75. template<typename T>
  76. Vector2 operator-=(const T f)
  77. {
  78. x -= f;
  79. y -= f;
  80. return *this;
  81. }
  82.  
  83. template<typename T>
  84. Vector2 operator*=(const Vector2<T> & other)
  85. {
  86. x *= other.x;
  87. y *= other.y;
  88. return *this;
  89. }
  90.  
  91. template<typename T>
  92. Vector2 operator*=(const T f)
  93. {
  94. x *= f;
  95. y *= f;
  96. return *this;
  97. }
  98.  
  99. template<typename T>
  100. Vector2 operator/=(const Vector2<T> & other)
  101. {
  102. x /= other.x;
  103. y /= other.y;
  104. return *this;
  105. }
  106.  
  107. template<typename T>
  108. Vector2 operator/=(const T f)
  109. {
  110. x /= f;
  111. y /= f;
  112. return *this;
  113. }
  114.  
  115. template<typename T>
  116. Vector2 operator+(const Vector2<T> & other) const
  117. {
  118. return (x + other.x, y + other.y);
  119. }
  120.  
  121. template<typename T>
  122. Vector2 operator+(const T f) const
  123. {
  124. return (x + f, y + f);
  125. }
  126.  
  127. template<typename T>
  128. Vector2 operator-(const Vector2<T> & other) const
  129. {
  130. return (x - other.x, y - other.y);
  131. }
  132.  
  133. template<typename T>
  134. Vector2 operator-(const T f) const
  135. {
  136. return (x - f, y - f);
  137. }
  138.  
  139. template<typename T>
  140. Vector2 operator*(const Vector2<T> & other) const
  141. {
  142. return (x * other.x, y * other.y);
  143. }
  144.  
  145. template<typename T>
  146. Vector2 operator*(const T f) const
  147. {
  148. return (x * f, y * f);
  149. }
  150.  
  151. template<typename T>
  152. Vector2 operator/(const Vector2<T> & other) const
  153. {
  154. return (x / other.x, y / other.y);
  155. }
  156.  
  157. template<typename T>
  158. Vector2 operator/(const T f) const
  159. {
  160. return (x / f, y / f);
  161. }
  162.  
  163. };
  164.  
  165. #endif // VECTOR2_H
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:11:11: error: declaration of 'class T'
  template<typename T>
           ^
prog.cpp:6:10: error:  shadows template parm 'class T'
 template<typename T>
          ^
prog.cpp:19:11: error: declaration of 'class T'
  template<typename T>
           ^
prog.cpp:6:10: error:  shadows template parm 'class T'
 template<typename T>
          ^
prog.cpp:27:11: error: declaration of 'class T'
  template<typename T>
           ^
prog.cpp:6:10: error:  shadows template parm 'class T'
 template<typename T>
          ^
prog.cpp:33:11: error: declaration of 'class T'
  template<typename T>
           ^
prog.cpp:6:10: error:  shadows template parm 'class T'
 template<typename T>
          ^
prog.cpp:39:11: error: declaration of 'class T'
  template<typename T>
           ^
prog.cpp:6:10: error:  shadows template parm 'class T'
 template<typename T>
          ^
prog.cpp:45:11: error: declaration of 'class T'
  template<typename T>
           ^
prog.cpp:6:10: error:  shadows template parm 'class T'
 template<typename T>
          ^
prog.cpp:51:11: error: declaration of 'class T'
  template<typename T>
           ^
prog.cpp:6:10: error:  shadows template parm 'class T'
 template<typename T>
          ^
prog.cpp:59:11: error: declaration of 'class T'
  template<typename T>
           ^
prog.cpp:6:10: error:  shadows template parm 'class T'
 template<typename T>
          ^
prog.cpp:67:11: error: declaration of 'class T'
  template<typename T>
           ^
prog.cpp:6:10: error:  shadows template parm 'class T'
 template<typename T>
          ^
prog.cpp:75:11: error: declaration of 'class T'
  template<typename T>
           ^
prog.cpp:6:10: error:  shadows template parm 'class T'
 template<typename T>
          ^
prog.cpp:83:11: error: declaration of 'class T'
  template<typename T>
           ^
prog.cpp:6:10: error:  shadows template parm 'class T'
 template<typename T>
          ^
prog.cpp:91:11: error: declaration of 'class T'
  template<typename T>
           ^
prog.cpp:6:10: error:  shadows template parm 'class T'
 template<typename T>
          ^
prog.cpp:99:11: error: declaration of 'class T'
  template<typename T>
           ^
prog.cpp:6:10: error:  shadows template parm 'class T'
 template<typename T>
          ^
prog.cpp:107:11: error: declaration of 'class T'
  template<typename T>
           ^
prog.cpp:6:10: error:  shadows template parm 'class T'
 template<typename T>
          ^
prog.cpp:115:11: error: declaration of 'class T'
  template<typename T>
           ^
prog.cpp:6:10: error:  shadows template parm 'class T'
 template<typename T>
          ^
prog.cpp:121:11: error: declaration of 'class T'
  template<typename T>
           ^
prog.cpp:6:10: error:  shadows template parm 'class T'
 template<typename T>
          ^
prog.cpp:127:11: error: declaration of 'class T'
  template<typename T>
           ^
prog.cpp:6:10: error:  shadows template parm 'class T'
 template<typename T>
          ^
prog.cpp:133:11: error: declaration of 'class T'
  template<typename T>
           ^
prog.cpp:6:10: error:  shadows template parm 'class T'
 template<typename T>
          ^
prog.cpp:139:11: error: declaration of 'class T'
  template<typename T>
           ^
prog.cpp:6:10: error:  shadows template parm 'class T'
 template<typename T>
          ^
prog.cpp:145:11: error: declaration of 'class T'
  template<typename T>
           ^
prog.cpp:6:10: error:  shadows template parm 'class T'
 template<typename T>
          ^
prog.cpp:151:11: error: declaration of 'class T'
  template<typename T>
           ^
prog.cpp:6:10: error:  shadows template parm 'class T'
 template<typename T>
          ^
prog.cpp:157:11: error: declaration of 'class T'
  template<typename T>
           ^
prog.cpp:6:10: error:  shadows template parm 'class T'
 template<typename T>
          ^
stdout
Standard output is empty