fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <memory>
  4.  
  5. class MatrixInterface
  6. {
  7. public:
  8. virtual ~MatrixInterface() = default;
  9.  
  10. virtual void doSomething() const = 0;
  11.  
  12. virtual MatrixInterface doSomethingElse() const = 0;
  13. };
  14.  
  15. template<int rows, int cols> class MatrixA : public MatrixInterface
  16. {
  17. public:
  18. void doSomething() const override
  19. {
  20. std::cout << "This is MatrixA... Rows:" << rows << "Cols:" << cols << std::endl;
  21. }
  22.  
  23. MatrixA<rows, cols> doSomethingElse() const override
  24. {
  25. return MatrixA<rows, cols>();
  26. }
  27. };
  28.  
  29. template<int rows, int cols>class MatrixB : public MatrixInterface
  30. {
  31. void doSomething() const override
  32. {
  33. std::cout << "This is MatrixB... Rows:" << rows << "Cols:" << cols << std::endl;
  34. }
  35. };
  36.  
  37. template<int rows, int cols>class MatrixC : public MatrixInterface
  38. {
  39. void doSomething() const override
  40. {
  41. std::cout << "This is MatrixC... Rows:" << rows << "Cols:" << cols << std::endl;
  42. }
  43. };
  44.  
  45. int main() {
  46. using VectorType = std::vector<std::unique_ptr<MatrixInterface>>;
  47. VectorType matricies;
  48.  
  49. matricies.push_back(VectorType::value_type(new MatrixA<3, 4>));
  50. matricies.push_back(VectorType::value_type(new MatrixB<5, 6>));
  51. matricies.push_back(VectorType::value_type(new MatrixC<7, 8>));
  52.  
  53. for(auto& element : matricies)
  54. {
  55. element->doSomething();
  56. }
  57.  
  58. return 0;
  59. }
Compilation error #stdin compilation error #stdout 0s 3480KB
stdin
Standard input is empty
compilation info
prog.cpp:12:26: error: invalid abstract return type for member function ‘virtual MatrixInterface MatrixInterface::doSomethingElse() const’
  virtual MatrixInterface doSomethingElse() const = 0;
                          ^
prog.cpp:5:7: note:   because the following virtual functions are pure within ‘MatrixInterface’:
 class MatrixInterface
       ^
prog.cpp:10:15: note: 	virtual void MatrixInterface::doSomething() const
  virtual void doSomething() const = 0;
               ^
prog.cpp:12:26: note: 	virtual MatrixInterface MatrixInterface::doSomethingElse() const
  virtual MatrixInterface doSomethingElse() const = 0;
                          ^
prog.cpp: In instantiation of ‘class MatrixA<3, 4>’:
prog.cpp:49:49:   required from here
prog.cpp:23:22: error: invalid covariant return type for ‘MatrixA<rows, cols> MatrixA<rows, cols>::doSomethingElse() const [with int rows = 3; int cols = 4]’
  MatrixA<rows, cols> doSomethingElse() const override
                      ^
prog.cpp:12:26: error:   overriding ‘virtual MatrixInterface MatrixInterface::doSomethingElse() const’
  virtual MatrixInterface doSomethingElse() const = 0;
                          ^
prog.cpp: In function ‘int main()’:
prog.cpp:50:49: error: cannot allocate an object of abstract type ‘MatrixB<5, 6>’
  matricies.push_back(VectorType::value_type(new MatrixB<5, 6>));
                                                 ^
prog.cpp:29:35: note:   because the following virtual functions are pure within ‘MatrixB<5, 6>’:
 template<int rows, int cols>class MatrixB : public MatrixInterface
                                   ^
prog.cpp:12:26: note: 	virtual MatrixInterface MatrixInterface::doSomethingElse() const
  virtual MatrixInterface doSomethingElse() const = 0;
                          ^
prog.cpp:51:49: error: cannot allocate an object of abstract type ‘MatrixC<7, 8>’
  matricies.push_back(VectorType::value_type(new MatrixC<7, 8>));
                                                 ^
prog.cpp:37:35: note:   because the following virtual functions are pure within ‘MatrixC<7, 8>’:
 template<int rows, int cols>class MatrixC : public MatrixInterface
                                   ^
prog.cpp:12:26: note: 	virtual MatrixInterface MatrixInterface::doSomethingElse() const
  virtual MatrixInterface doSomethingElse() const = 0;
                          ^
stdout
Standard output is empty