fork(1) download
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. const size_t SIZE =4;
  7.  
  8. template<typename T>
  9. class Matrix
  10. {
  11. private:
  12. T arr[SIZE][SIZE];
  13. friend ostream& operator << (ostream &, const Matrix &);
  14. friend istream& operator >> (istream &, Matrix &);
  15. void initialize();// функция которая заполняет матрицу как единичную
  16.  
  17. public:
  18. Matrix() {}
  19. Matrix(const Matrix<T>&);
  20. const Matrix& operator=(const Matrix<T>&);
  21. const Matrix& operator*(const Matrix<T>&);
  22. void operator*=(const Matrix<T> &);
  23. T* operator[](int row);
  24. };
  25.  
  26. template< typename T>
  27. ostream & operator<<(ostream & os, const Matrix<T> & rhs)
  28. {
  29. for (int i(0); i < SIZE; ++i)
  30. {
  31. for (int j(0); j < SIZE; ++j)
  32. {
  33. os << rhs.arr[i][j] << ' ';
  34. }
  35. os << endl;
  36. }
  37. return os;
  38. }
  39.  
  40. template< typename T>
  41. istream & operator>>(istream& is, Matrix<T> & rhs)
  42. {
  43. for (int i(0); i < SIZE; ++i)
  44. {
  45. for (int j(0); j < SIZE; ++j)
  46. {
  47. is >> rhs.arr[i][j];
  48. }
  49. }
  50. return is;
  51. }
  52.  
  53. int main()
  54. {
  55. Matrix <int> m;
  56. ifstream("input.txt") >> m;
  57.  
  58. return 0;
  59. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:13:60: warning: friend declaration 'std::ostream& operator<<(std::ostream&, const Matrix<T>&)' declares a non-template function [-Wnon-template-friend]
     friend  ostream& operator << (ostream &, const Matrix &);
                                                            ^
prog.cpp:13:60: note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) 
prog.cpp:14:54: warning: friend declaration 'std::istream& operator>>(std::istream&, Matrix<T>&)' declares a non-template function [-Wnon-template-friend]
     friend  istream& operator >> (istream &, Matrix &);
                                                      ^
/home/qLEnJR/ccfFikXL.o: In function `main':
prog.cpp:(.text.startup+0x34): undefined reference to `operator>>(std::istream&, Matrix<int>&)'
collect2: error: ld returned 1 exit status
stdout
Standard output is empty