fork download
  1. #include <vector>
  2.  
  3. template <typename T>
  4. class matrix
  5. {
  6. std::vector< std::vector<T> > data;
  7. size_t N, M;
  8. public:
  9. T& at(size_t x, size_t y);
  10. matrix<T> operator+(const matrix<T>& m2) const;
  11. };
  12.  
  13. template<class T>
  14. T& matrix<T>::at(size_t x, size_t y)
  15. { return data[x][y]; }
  16.  
  17. template<class T>
  18. matrix<T> matrix<T>::operator+(const matrix<T>& m2) const
  19. {
  20. matrix<T> res;
  21. for(unsigned int i=0; i<N; ++i)
  22. for(unsigned int j=0; j<M; ++j)
  23. res.at(i,j) = this->at(i, j) + m2.at(i, j);
  24. return res;
  25. }
  26.  
  27. int main()
  28. {
  29. matrix<int> a, b;
  30. a = a + b;
  31. return 0;
  32. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In member function 'matrix<T> matrix<T>::operator+(const matrix<T>&) const [with T = int]':
prog.cpp:30:13:   instantiated from here
prog.cpp:23:13: error: passing 'const matrix<int>' as 'this' argument of 'T& matrix<T>::at(size_t, size_t) [with T = int, size_t = unsigned int]' discards qualifiers
prog.cpp:30:13:   instantiated from here
prog.cpp:23:13: error: passing 'const matrix<int>' as 'this' argument of 'T& matrix<T>::at(size_t, size_t) [with T = int, size_t = unsigned int]' discards qualifiers
stdout
Standard output is empty