fork download
  1. //Code for ArrayClass.h
  2. #include "AbstractArray.h"
  3. template <class DT>
  4.  
  5. class ArrayClass : virtual public AbstractArray<DT>
  6. {
  7. protected:
  8. // An array of type Object will be created and paObject will be
  9. // the address of the array.
  10. DT* paObject;
  11. int _size;
  12. void copy (const ArrayClass<DT>& ac);
  13. public:
  14. ArrayClass ();
  15. ArrayClass (int n);
  16. ArrayClass (int n, const DT& val);
  17. ArrayClass (const ArrayClass<DT>& ac);
  18. virtual ~ArrayClass ();
  19. virtual int size () const;
  20. virtual DT& operator [] (int k);
  21. void operator= (const ArrayClass<DT>& ac);
  22. };
  23. //Code for ArrayClass.cpp
  24.  
  25. #include "ArrayClass.h"
  26.  
  27. template <class DT>
  28. ArrayClass<DT>::ArrayClass ()
  29. {
  30. _size = 0; // default in case allocation fails
  31. paObject = new DT[1];
  32. if (paObject == NULL) throw ArrayMemoryException();
  33. _size = 1;
  34. }
  35. template <class DT>
  36. ArrayClass<DT>::ArrayClass (int n)
  37. {
  38. _size = 0; // default in case allocation fails
  39. paObject = new DT[n];
  40. if (paObject == NULL) throw ArrayMemoryException();
  41. _size = n;
  42. }
  43.  
  44. template<class DT>
  45. ArrayClass<DT>::ArrayClass (int n, const DT& val)
  46. {
  47. int i;
  48. _size = 0; // default in case allocation fails
  49. paObject = new DT[n];
  50. if (paObject == NULL) throw ArrayMemoryException();
  51. _size = n;
  52. for (int i = 0; i < n; i++)
  53. paObject[i] = val;
  54. }
  55. template <class DT>
  56. ArrayClass<DT>::ArrayClass (const ArrayClass<DT>& ac)
  57. {
  58. copy (ac);
  59. }
  60.  
  61. template <class DT>
  62. int ArrayClass<DT>::size () const
  63. {
  64. return _size;
  65. }
  66.  
  67. template <class DT>
  68. DT& ArrayClass<DT>::operator [] (int k)
  69. {
  70. if ((k < 0) || (k >= size())) throw ArrayBoundsException();
  71. return paObject[k];
  72. }
  73. template <class DT>
  74. void ArrayClass<DT>::operator= (const ArrayClass<DT>& ac)
  75. {
  76. if (paObject != NULL)
  77. delete[] paObject;
  78. copy (ac);
  79. }
  80. template<class DT>
  81. std::ostream& operator<< (ostream& s, AbstractArray<DT>& ac)
  82. {
  83. s << "[";
  84. for (int i = 0; i < ac.size (); i++)
  85. {
  86. if (i > 0)
  87. {
  88. s << ',';
  89. }
  90. s << ac [i];
  91. }
  92. s << "]";
  93. return s;
  94. }
  95. template <class DT>
  96. void ArrayClass<DT>::copy (const ArrayClass<DT>& ac)
  97. {
  98. _size = 0; // default in case allocation fails
  99. paObject = new DT[ac._size];
  100. if (paObject == NULL) throw ArrayMemoryException();
  101. _size = ac._size;
  102. for (int i = 0; i < _size; i++)
  103. {
  104. paObject[i] = ac.paObject[i];
  105. }
  106. }
  107. //Code for AbstractVector.h
  108.  
  109. template <class DT>
  110. class AbstractVector : virtual public AbstractArray<DT>
  111. {
  112. public:
  113. virtual void insert (const DT& item, int index) =
  114. NULL;
  115. //insert a new DT at position index in the vector.
  116. virtual void remove (int index) = NULL;
  117. //remove the object at position index of the vector.
  118. virtual void add (const DT& item) = NULL;
  119. // adds item at the end of the Vector
  120. };
  121. //Code for AbstractArray.h
  122. #include <iostream>
  123. using namespace std;
  124.  
  125. class Exception { };
  126. class ArrayException : public Exception { };
  127. class ArrayMemoryException : public ArrayException { };
  128. class ArrayBoundsException : public ArrayException { };
  129.  
  130. template <class DT>
  131. class AbstractArray
  132. {
  133. public:
  134. virtual int size () const = NULL;
  135. virtual DT& operator [] (int k) = NULL;
  136. friend std::ostream& operator<< (ostream& s, AbstractArray<DT>& ac);
  137. };
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:2:27: error: AbstractArray.h: No such file or directory
prog.cpp:25:24: error: ArrayClass.h: No such file or directory
prog.cpp:5: error: expected template-name before ‘<’ token
prog.cpp:5: error: expected `{' before ‘<’ token
prog.cpp:5: error: expected unqualified-id before ‘<’ token
prog.cpp:28: error: invalid use of incomplete type ‘class ArrayClass<DT>’
prog.cpp:5: error: declaration of ‘class ArrayClass<DT>’
prog.cpp:36: error: invalid use of incomplete type ‘class ArrayClass<DT>’
prog.cpp:5: error: declaration of ‘class ArrayClass<DT>’
prog.cpp:45: error: invalid use of incomplete type ‘class ArrayClass<DT>’
prog.cpp:5: error: declaration of ‘class ArrayClass<DT>’
prog.cpp:56: error: invalid use of incomplete type ‘class ArrayClass<DT>’
prog.cpp:5: error: declaration of ‘class ArrayClass<DT>’
prog.cpp:62: error: invalid use of incomplete type ‘class ArrayClass<DT>’
prog.cpp:5: error: declaration of ‘class ArrayClass<DT>’
prog.cpp:68: error: invalid use of incomplete type ‘class ArrayClass<DT>’
prog.cpp:5: error: declaration of ‘class ArrayClass<DT>’
prog.cpp:74: error: invalid use of incomplete type ‘class ArrayClass<DT>’
prog.cpp:5: error: declaration of ‘class ArrayClass<DT>’
prog.cpp:81: error: expected constructor, destructor, or type conversion before ‘&’ token
prog.cpp:96: error: invalid use of incomplete type ‘class ArrayClass<DT>’
prog.cpp:5: error: declaration of ‘class ArrayClass<DT>’
prog.cpp:110: error: expected template-name before ‘<’ token
prog.cpp:110: error: expected `{' before ‘<’ token
prog.cpp:110: error: expected unqualified-id before ‘<’ token
prog.cpp:134: error: invalid pure specifier (only `= 0' is allowed) before ‘;’ token
prog.cpp:135: error: invalid pure specifier (only `= 0' is allowed) before ‘;’ token
prog.cpp:136: warning: friend declaration ‘std::ostream& operator<<(std::ostream&, AbstractArray<DT>&)’ declares a non-template function
prog.cpp:136: note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) 
stdout
Standard output is empty