fork download
  1. #include <iostream>
  2. #include <functional>
  3. #include <cstdlib>
  4.  
  5. template <class T>
  6. class position
  7. {
  8. public:
  9. position(T a, T b) : x(a), y(b) {}
  10. position(const position &a) : x(a.x), y(a.y) {}
  11. position& operator=(position rhs)
  12. {
  13. swap(rhs);
  14. return *this;
  15. }
  16. void swap(position &rhs)
  17. {
  18. std::swap(x, rhs.x);
  19. std::swap(y, rhs.y);
  20. }
  21.  
  22. position& operator+=(const position &rhs)
  23. {
  24. return perform_compound_operator(*this, rhs, std::plus<T>());
  25. }
  26. position operator+(const position &rhs) const
  27. {
  28. return perform_arithmetic_operator(*this, rhs, std::plus<T>());
  29. }
  30.  
  31. position& operator-=(const position &rhs)
  32. {
  33. return perform_compound_operator(*this, rhs, std::minus<T>());
  34. }
  35. position operator-(const position &rhs) const
  36. {
  37. return perform_arithmetic_operator(*this, rhs, std::minus<T>());
  38. }
  39.  
  40. position& operator*=(const position &rhs)
  41. {
  42. return perform_compound_operator(*this, rhs, std::multiplies<T>());
  43. }
  44. position operator*(const position &rhs) const
  45. {
  46. return perform_arithmetic_operator(*this, rhs, std::multiplies<T>());
  47. }
  48.  
  49. position& operator/=(const position &rhs)
  50. {
  51. return perform_compound_operator(*this, rhs, std::divides<T>());
  52. }
  53. position operator/(const position &rhs) const
  54. {
  55. return perform_arithmetic_operator(*this, rhs, std::divides<T>());
  56. }
  57.  
  58. bool operator==(const position &rhs) const
  59. {
  60. return x == rhs.x && y == rhs.y;
  61. }
  62. bool operator!=(const position &rhs) const
  63. {
  64. return !(*this == rhs);
  65. }
  66.  
  67. friend std::ostream& operator<<(std::ostream &os, const position &pos)
  68. {
  69. os << '(' << pos.x << ',' << pos.y << ')';
  70. return os;
  71. }
  72.  
  73. private:
  74. template <typename A>
  75. static position& perform_compound_operator(position &lhs, const position &rhs, A &arithmetic)
  76. {
  77. lhs.x = arithmetic(lhs.x, rhs.x);
  78. lhs.y = arithmetic(lhs.y, rhs.y);
  79. return lhs;
  80. }
  81.  
  82. template <typename A>
  83. static position perform_arithmetic_operator(position lhs, const position &rhs, A &arithmetic)
  84. {
  85. perform_compound_operator(lhs, rhs, arithmetic);
  86. return lhs;
  87. }
  88.  
  89. T x;
  90. T y;
  91. };
  92.  
  93. int main()
  94. {
  95. using namespace std;
  96.  
  97. position<double> a(1.0, 1.0), b(2.23, 0.34), c(2.2, 1.1);
  98. a = ((b*c) - a + c) / b;
  99. cout << "a的座標為" << a << std::endl;
  100. cout << "b的座標為" << b << std::endl;
  101. cout << "c的座標為" << c << std::endl;
  102.  
  103. //std::system("pause");
  104. return 0;
  105. }
  106.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In instantiation of ‘position<T> position<T>::operator*(const position<T>&) const [with T = double]’:
prog.cpp:98:10:   required from here
prog.cpp:46:70: error: no matching function for call to ‘position<double>::perform_arithmetic_operator(const position<double>&, const position<double>&, std::multiplies<double>) const’
   return perform_arithmetic_operator(*this, rhs, std::multiplies<T>());
                                                                      ^
prog.cpp:46:70: note: candidate is:
prog.cpp:83:18: note: static position<T> position<T>::perform_arithmetic_operator(position<T>, const position<T>&, A&) [with A = std::multiplies<double>; T = double]
  static position perform_arithmetic_operator(position lhs, const position &rhs, A &arithmetic)
                  ^
prog.cpp:83:18: note:   no known conversion for argument 3 from ‘std::multiplies<double>’ to ‘std::multiplies<double>&’
prog.cpp: In instantiation of ‘position<T> position<T>::operator-(const position<T>&) const [with T = double]’:
prog.cpp:98:15:   required from here
prog.cpp:37:65: error: no matching function for call to ‘position<double>::perform_arithmetic_operator(const position<double>&, const position<double>&, std::minus<double>) const’
   return perform_arithmetic_operator(*this, rhs, std::minus<T>());
                                                                 ^
prog.cpp:37:65: note: candidate is:
prog.cpp:83:18: note: static position<T> position<T>::perform_arithmetic_operator(position<T>, const position<T>&, A&) [with A = std::minus<double>; T = double]
  static position perform_arithmetic_operator(position lhs, const position &rhs, A &arithmetic)
                  ^
prog.cpp:83:18: note:   no known conversion for argument 3 from ‘std::minus<double>’ to ‘std::minus<double>&’
prog.cpp: In instantiation of ‘position<T> position<T>::operator+(const position<T>&) const [with T = double]’:
prog.cpp:98:19:   required from here
prog.cpp:28:64: error: no matching function for call to ‘position<double>::perform_arithmetic_operator(const position<double>&, const position<double>&, std::plus<double>) const’
   return perform_arithmetic_operator(*this, rhs, std::plus<T>());
                                                                ^
prog.cpp:28:64: note: candidate is:
prog.cpp:83:18: note: static position<T> position<T>::perform_arithmetic_operator(position<T>, const position<T>&, A&) [with A = std::plus<double>; T = double]
  static position perform_arithmetic_operator(position lhs, const position &rhs, A &arithmetic)
                  ^
prog.cpp:83:18: note:   no known conversion for argument 3 from ‘std::plus<double>’ to ‘std::plus<double>&’
prog.cpp: In instantiation of ‘position<T> position<T>::operator/(const position<T>&) const [with T = double]’:
prog.cpp:98:24:   required from here
prog.cpp:55:67: error: no matching function for call to ‘position<double>::perform_arithmetic_operator(const position<double>&, const position<double>&, std::divides<double>) const’
   return perform_arithmetic_operator(*this, rhs, std::divides<T>());
                                                                   ^
prog.cpp:55:67: note: candidate is:
prog.cpp:83:18: note: static position<T> position<T>::perform_arithmetic_operator(position<T>, const position<T>&, A&) [with A = std::divides<double>; T = double]
  static position perform_arithmetic_operator(position lhs, const position &rhs, A &arithmetic)
                  ^
prog.cpp:83:18: note:   no known conversion for argument 3 from ‘std::divides<double>’ to ‘std::divides<double>&’
prog.cpp: In member function ‘position<T> position<T>::operator*(const position<T>&) const [with T = double]’:
prog.cpp:47:2: warning: control reaches end of non-void function [-Wreturn-type]
  }
  ^
prog.cpp: In member function ‘position<T> position<T>::operator-(const position<T>&) const [with T = double]’:
prog.cpp:38:2: warning: control reaches end of non-void function [-Wreturn-type]
  }
  ^
prog.cpp: In member function ‘position<T> position<T>::operator+(const position<T>&) const [with T = double]’:
prog.cpp:29:2: warning: control reaches end of non-void function [-Wreturn-type]
  }
  ^
prog.cpp: In member function ‘position<T> position<T>::operator/(const position<T>&) const [with T = double]’:
prog.cpp:56:2: warning: control reaches end of non-void function [-Wreturn-type]
  }
  ^
stdout
Standard output is empty