fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class IntArr
  5. {
  6. private:
  7. int* ptrArr;
  8. int len;
  9.  
  10. public:
  11. IntArr(int len);
  12. ~IntArr();
  13.  
  14. int length() const { return len;}
  15. void compress();
  16. };
  17.  
  18. IntArr::IntArr(int len)
  19. {
  20. this->len = len;
  21. ptrArr = new int[len];
  22. for (int i = 0; i < len; i++)
  23. {
  24. ptrArr[i] = 0;
  25. }
  26. }
  27.  
  28. IntArr::~IntArr()
  29. {
  30. delete [] ptrArr;
  31. }
  32.  
  33. void IntArr::compress()
  34. {
  35. int count=0;
  36. int index=0;
  37.  
  38. for (int i = 0; i < len; i++)
  39. {
  40. if (ptrArr[i] != 0)
  41. count++;
  42. }
  43.  
  44. int* ptrArr1 = new int[count];
  45.  
  46. for (int i = 0; i < len; i++)
  47. {
  48. if (ptrArr[i] != 0)
  49. ptrArr1[index] = ptrArr[i];
  50. index += 1;
  51. }
  52.  
  53. delete[] ptrArr;
  54. ptrArr = ptrArr1;
  55. len = count;
  56. }
  57.  
  58. int main()
  59. {
  60. IntArr arr(100);
  61.  
  62. srand((unsigned int)time(NULL));
  63.  
  64. for (int i = 0; i < arr.length(); i++)
  65. {
  66. arr[i] = rand();
  67. }
  68. }
  69.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:66:12: error: no match for ‘operator[]’ (operand types are ‘IntArr’ and ‘int’)
         arr[i] = rand();
            ^
stdout
Standard output is empty