fork(2) download
  1. #include <iostream>
  2. using namespace std;
  3. class Fraction {
  4. private:
  5. int num, den;
  6. public:
  7. Fraction(int n = 0, int d = 1) :num(n), den(d) {}
  8.  
  9. void setFraction(int n, int d) {
  10. num = n;
  11. den = d;
  12. }
  13. void printFraction() {
  14. cout << num << '/' << den << " ";
  15. }
  16. friend Fraction operator+(Fraction&, Fraction&);
  17. };
  18.  
  19. int main()
  20. {
  21. Fraction f1(1, 2), f2;
  22.  
  23. f2 = f1 + f1 + f1; //fail
  24. f2.printFraction();
  25.  
  26. f2 = f1 + f1; //pass
  27. f2.printFraction();
  28. system("pause");
  29. return 0;
  30. }
  31. Fraction operator+(Fraction& a, Fraction& b) {
  32. Fraction tmp;
  33. int den = a.den * b.den;
  34. int num = a.num * b.den + b.num * a.den;
  35. tmp.setFraction(num, den);
  36. return tmp;
  37. }
Compilation error #stdin compilation error #stdout 0.01s 5548KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:24:15: error: no match for ‘operator+’ (operand types are ‘Fraction’ and ‘Fraction’)
  f2 = f1 + f1 + f1; //fail
       ~~~~~~~~^~~~
In file included from /usr/include/c++/8/bits/stl_algobase.h:67,
                 from /usr/include/c++/8/bits/char_traits.h:39,
                 from /usr/include/c++/8/ios:40,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/8/bits/stl_iterator.h:400:5: note: candidate: ‘template<class _Iterator> std::reverse_iterator<_Iterator> std::operator+(typename std::reverse_iterator<_Iterator>::difference_type, const std::reverse_iterator<_Iterator>&)’
     operator+(typename reverse_iterator<_Iterator>::difference_type __n,
     ^~~~~~~~
/usr/include/c++/8/bits/stl_iterator.h:400:5: note:   template argument deduction/substitution failed:
prog.cpp:24:17: note:   ‘Fraction’ is not derived from ‘const std::reverse_iterator<_Iterator>’
  f2 = f1 + f1 + f1; //fail
                 ^~
In file included from /usr/include/c++/8/bits/stl_algobase.h:67,
                 from /usr/include/c++/8/bits/char_traits.h:39,
                 from /usr/include/c++/8/ios:40,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/8/bits/stl_iterator.h:1195:5: note: candidate: ‘template<class _Iterator> std::move_iterator<_IteratorL> std::operator+(typename std::move_iterator<_IteratorL>::difference_type, const std::move_iterator<_IteratorL>&)’
     operator+(typename move_iterator<_Iterator>::difference_type __n,
     ^~~~~~~~
/usr/include/c++/8/bits/stl_iterator.h:1195:5: note:   template argument deduction/substitution failed:
prog.cpp:24:17: note:   ‘Fraction’ is not derived from ‘const std::move_iterator<_IteratorL>’
  f2 = f1 + f1 + f1; //fail
                 ^~
In file included from /usr/include/c++/8/string:52,
                 from /usr/include/c++/8/bits/locale_classes.h:40,
                 from /usr/include/c++/8/bits/ios_base.h:41,
                 from /usr/include/c++/8/ios:42,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/8/bits/basic_string.h:5927:5: note: candidate: ‘template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)’
     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
     ^~~~~~~~
/usr/include/c++/8/bits/basic_string.h:5927:5: note:   template argument deduction/substitution failed:
prog.cpp:24:17: note:   ‘Fraction’ is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
  f2 = f1 + f1 + f1; //fail
                 ^~
In file included from /usr/include/c++/8/string:53,
                 from /usr/include/c++/8/bits/locale_classes.h:40,
                 from /usr/include/c++/8/bits/ios_base.h:41,
                 from /usr/include/c++/8/ios:42,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/8/bits/basic_string.tcc:1157:5: note: candidate: ‘template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(const _CharT*, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)’
     operator+(const _CharT* __lhs,
     ^~~~~~~~
/usr/include/c++/8/bits/basic_string.tcc:1157:5: note:   template argument deduction/substitution failed:
prog.cpp:24:17: note:   mismatched types ‘const _CharT*’ and ‘Fraction’
  f2 = f1 + f1 + f1; //fail
                 ^~
In file included from /usr/include/c++/8/string:53,
                 from /usr/include/c++/8/bits/locale_classes.h:40,
                 from /usr/include/c++/8/bits/ios_base.h:41,
                 from /usr/include/c++/8/ios:42,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/8/bits/basic_string.tcc:1173:5: note: candidate: ‘template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(_CharT, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)’
     operator+(_CharT __lhs, const basic_string<_CharT, _Traits, _Alloc>& __rhs)
     ^~~~~~~~
/usr/include/c++/8/bits/basic_string.tcc:1173:5: note:   template argument deduction/substitution failed:
prog.cpp:24:17: note:   ‘Fraction’ is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
  f2 = f1 + f1 + f1; //fail
                 ^~
In file included from /usr/include/c++/8/string:52,
                 from /usr/include/c++/8/bits/locale_classes.h:40,
                 from /usr/include/c++/8/bits/ios_base.h:41,
                 from /usr/include/c++/8/ios:42,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/8/bits/basic_string.h:5964:5: note: candidate: ‘template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)’
     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
     ^~~~~~~~
/usr/include/c++/8/bits/basic_string.h:5964:5: note:   template argument deduction/substitution failed:
prog.cpp:24:17: note:   ‘Fraction’ is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
  f2 = f1 + f1 + f1; //fail
                 ^~
In file included from /usr/include/c++/8/string:52,
                 from /usr/include/c++/8/bits/locale_classes.h:40,
                 from /usr/include/c++/8/bits/ios_base.h:41,
                 from /usr/include/c++/8/ios:42,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/8/bits/basic_string.h:5980:5: note: candidate: ‘template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, _CharT)’
     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
     ^~~~~~~~
/usr/include/c++/8/bits/basic_string.h:5980:5: note:   template argument deduction/substitution failed:
prog.cpp:24:17: note:   ‘Fraction’ is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
  f2 = f1 + f1 + f1; //fail
                 ^~
In file included from /usr/include/c++/8/string:52,
                 from /usr/include/c++/8/bits/locale_classes.h:40,
                 from /usr/include/c++/8/bits/ios_base.h:41,
                 from /usr/include/c++/8/ios:42,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/8/bits/basic_string.h:5992:5: note: candidate: ‘template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)’
     operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
     ^~~~~~~~
/usr/include/c++/8/bits/basic_string.h:5992:5: note:   template argument deduction/substitution failed:
prog.cpp:24:17: note:   ‘Fraction’ is not derived from ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
  f2 = f1 + f1 + f1; //fail
                 ^~
In file included from /usr/include/c++/8/string:52,
                 from /usr/include/c++/8/bits/locale_classes.h:40,
                 from /usr/include/c++/8/bits/ios_base.h:41,
                 from /usr/include/c++/8/ios:42,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/8/bits/basic_string.h:5998:5: note: candidate: ‘template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&)’
     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
     ^~~~~~~~
/usr/include/c++/8/bits/basic_string.h:5998:5: note:   template argument deduction/substitution failed:
prog.cpp:24:17: note:   ‘Fraction’ is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
  f2 = f1 + f1 + f1; //fail
                 ^~
In file included from /usr/include/c++/8/string:52,
                 from /usr/include/c++/8/bits/locale_classes.h:40,
                 from /usr/include/c++/8/bits/ios_base.h:41,
                 from /usr/include/c++/8/ios:42,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/8/bits/basic_string.h:6004:5: note: candidate: ‘template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&)’
     operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
     ^~~~~~~~
/usr/include/c++/8/bits/basic_string.h:6004:5: note:   template argument deduction/substitution failed:
prog.cpp:24:17: note:   ‘Fraction’ is not derived from ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
  f2 = f1 + f1 + f1; //fail
                 ^~
In file included from /usr/include/c++/8/string:52,
                 from /usr/include/c++/8/bits/locale_classes.h:40,
                 from /usr/include/c++/8/bits/ios_base.h:41,
                 from /usr/include/c++/8/ios:42,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/8/bits/basic_string.h:6016:5: note: candidate: ‘template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(const _CharT*, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&)’
     operator+(const _CharT* __lhs,
     ^~~~~~~~
/usr/include/c++/8/bits/basic_string.h:6016:5: note:   template argument deduction/substitution failed:
prog.cpp:24:17: note:   mismatched types ‘const _CharT*’ and ‘Fraction’
  f2 = f1 + f1 + f1; //fail
                 ^~
In file included from /usr/include/c++/8/string:52,
                 from /usr/include/c++/8/bits/locale_classes.h:40,
                 from /usr/include/c++/8/bits/ios_base.h:41,
                 from /usr/include/c++/8/ios:42,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/8/bits/basic_string.h:6022:5: note: candidate: ‘template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(_CharT, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&)’
     operator+(_CharT __lhs,
     ^~~~~~~~
/usr/include/c++/8/bits/basic_string.h:6022:5: note:   template argument deduction/substitution failed:
prog.cpp:24:17: note:   ‘Fraction’ is not derived from ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
  f2 = f1 + f1 + f1; //fail
                 ^~
In file included from /usr/include/c++/8/string:52,
                 from /usr/include/c++/8/bits/locale_classes.h:40,
                 from /usr/include/c++/8/bits/ios_base.h:41,
                 from /usr/include/c++/8/ios:42,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/8/bits/basic_string.h:6028:5: note: candidate: ‘template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&, const _CharT*)’
     operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
     ^~~~~~~~
/usr/include/c++/8/bits/basic_string.h:6028:5: note:   template argument deduction/substitution failed:
prog.cpp:24:17: note:   ‘Fraction’ is not derived from ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
  f2 = f1 + f1 + f1; //fail
                 ^~
In file included from /usr/include/c++/8/string:52,
                 from /usr/include/c++/8/bits/locale_classes.h:40,
                 from /usr/include/c++/8/bits/ios_base.h:41,
                 from /usr/include/c++/8/ios:42,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/8/bits/basic_string.h:6034:5: note: candidate: ‘template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&, _CharT)’
     operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
     ^~~~~~~~
/usr/include/c++/8/bits/basic_string.h:6034:5: note:   template argument deduction/substitution failed:
prog.cpp:24:17: note:   ‘Fraction’ is not derived from ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
  f2 = f1 + f1 + f1; //fail
                 ^~
prog.cpp:17:18: note: candidate: ‘Fraction operator+(Fraction&, Fraction&)’ <near match>
  friend Fraction operator+(Fraction&, Fraction&);
                  ^~~~~~~~
prog.cpp:17:18: note:   conversion of argument 1 would be ill-formed:
prog.cpp:24:10: error: cannot bind non-const lvalue reference of type ‘Fraction&’ to an rvalue of type ‘Fraction’
  f2 = f1 + f1 + f1; //fail
       ~~~^~~~
prog.cpp:29:8: warning: ignoring return value of ‘int system(const char*)’, declared with attribute warn_unused_result [-Wunused-result]
  system("pause");
  ~~~~~~^~~~~~~~~
stdout
Standard output is empty