Trying to make an ArrayList class, a class with a private dynamically array data-member and vector-like public functions, what am I doing wrong? (C++) [duplicate]

  c++

I keep getting errors whenever I try to define a size function, as well as for an insert function, for my ArrayList class.

ArrayList.h:

#ifndef ARRAYLIST_H
#define ARRAYLIST_H 

#include <cstdlib> 

// TODO(student): declare class here
class ArrayList{

  private:
    int size;
    int* arr;
    

  public: 

    ArrayList();
    
    int size() const;

    int at(int index) const;

    int insert(int index, int value);

    int remove(int index);
}

#endif  // ARRAYLIST_H

ArrayList.cpp:

#include <stdexcept> // Q: what is this "stdexcept", what does it all us to do?
#include "ArrayList.h"

// TODO(student): define class methods here

ArrayList::ArrayList(){
  int size = 1;
  int* arr = new int[size];

  }
  
int ArrayList::size() const {
  return size;
}

int ArrayList::at(int index) const {

  return arr[index];

}

int ArrayList::insert(int index, int value) {

  int* tempArr = new int[size + 1];
   

  for(int i = 0; i < (size + 1); i++){

    if(i < index){
      tempArr[i] = arr[i];
    }
    else if(i == index){
      tempArr[i] = value;
    }
    else if(i > index){
      tempArr[i] = arr[i - 1];
    }

    
  }

  delete [] arr;
  arr = tempArr;
  size++;



}

int ArrayList::remove(int index){

  int* tempArr = new int[size - 1];
   

  for(int i = 0; i < (size - 1); i++){

    if(i < index){
      tempArr[i] = arr[i];
    }
    else if(i >= index){
      tempArr[i] = arr[i + 1];
    }
    
  }

  delete [] arr;
  arr = tempArr;
  size--;

}

main.cpp:

/*
i/o tests coming soon

DO NOT CHANGE THIS FILE!
*/

#include <iostream>
#include "ArrayList.h"

using std::cin, std::endl, std::cout;

std::ostream& operator<<(std::ostream& os, const ArrayList& list) {
    os << "[";
    if (list.size() > 0) {
        os << list.at(0);
        for (size_t i = 1; i < list.size(); i++) {
            os << ", " << list.at(i);
        }
    }
    os << "]";
    return os;
}

bool is_even(int n) {
    return n % 2 == 0;
}

size_t insert_in_order(ArrayList& list, int value, size_t hint=0) {
    if (list.size() == 0) {
        list.insert(0, value);
        return 0;
    }
    if (hint < list.size() && value <= list.at(hint) && (hint == 0 || value >= list.at(hint-1))) {
        list.insert(hint, value);
        return hint;
    }
    size_t a = 0;
    size_t b = list.size();
    size_t m = hint;
    while (a < b) {
        if (value < list.at(m)) {
            b = m;
        } else if (value > list.at(m)) {
            a = m+1;
        } else {
            break;
        }
        m = (a + b) / 2;
    }
    list.insert(m, value);
    return m;
}

size_t remove_by_value(ArrayList& list, int value, size_t hint = 0) {
    if (hint < list.size() && list.at(hint) == value) {
        list.remove(hint);
        return hint;
    }
    for (size_t index = 0; index < list.size(); index++) {
        if (list.at(index) == value) {
            list.remove(index);
            return index;
        }
    }
    return list.size();
}

int main() {
    int value;
    cin >> value;
    
    ArrayList list;
    int n = value;
    while (n > 1) {
        insert_in_order(list, n);
        
        if (is_even(n)) {
            n /= 2;
        } else {
            (n *= 3) += 1;
        }
    }
    list.insert(0, 1);

    cout << list.size() << endl;
    cout << list << endl;
    
    try {
        cout << list.at(list.size()) << endl;
    } catch (const std::out_of_range& err) {
        cout << "OK" << endl;
    }
    
    n = value;
    while (n > 1) {
        remove_by_value(list, n);
        
        if (is_even(n)) {
            n /= 2;
        } else {
            (n *= 3) += 1;
        }
        cout << list.size() << endl;
        cout << list << endl;
    }
    list.remove(0);

    cout << list.size() << endl;
    cout << list << endl;

    try {
        list.remove(0);
        cout << "NO!" << endl;
    } catch (const std::out_of_range& err) {
        cout << "OK" << endl;
    }
}

As both functions are member function of the class, as seen in the screeshot below, shouldn’t they be able to access my class’s private data members?
Lastly, what does vs_code mean by "expected a declaration" in the screenshot directly above?
I have included links to my files below, any help on this would be greatly appreciated. Thank you!!

ArrayList.cpp

ArrayList.h

main.cpp

Source: Windows Questions C++

LEAVE A COMMENT