Custom Array Error || Insert() Error || DS&A Part 1 Exercise

I am taking the first part of Mosh’s data structures and algorithms course, and I am trying to create an adjustable array from scratch that can print, insert, find an index of, and remove at the index of. I am running into an error that I can’t find.

The Error:

Undefined symbol: Array<std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator>>::Array(int)

Array.cpp:

#include "Array.hpp"
#include <iostream>

template<typename T>
Array<T>::Array(int length) : length(length) {
    newArray = new T[length];
}

template<typename T>
void Array<T>::insert(T value) {
    if ( size(newArray) == length){
        T newerArray = new T[length++];
        for(int i = 0; i < size(newArray); i++){
            newerArray[i] = newArray[i];
        }
        delete[] newArray;
        newerArray[elem++] = value;
    } else {
        newArray[elem] = value;
        elem++;
    }
}

template<typename T>
void Array<T>::print(){
    for(T val : newArray)
        std::cout << val;
}

template<typename T>
void Array<T>::removeAt(int index){
    for (int i = index - 1; i < size(newArray) - 1; ++i)
        newArray[i] = newArray[i + 1];
}

template<typename T>
int Array<T>::indexOf(T value){
    for (int i = 0; i < size(newArray); i++){
        if (newArray[i] == value)
            return i;
    }
    return -1;
}

template<typename T>
Array<T>::~Array() {
    delete[] newArray;
}

Array.hpp:

template <typename T>
class Array {
public:
    Array(int length);
    ~Array();
    void insert(T value);
    void print();
    void removeAt(int index);
    int indexOf(T value);
private:
    T *newArray;
    int elem = 0;
    int length;
};

Main File:

#include <iostream>
#include "Array.hpp"
#include <string>

int main() {
    Array<std::string> array(3);
    
    return 0;
}

I have a question and a request to ask.

  1. How can I fix the error in this code?
  2. How can I decode the error I sent so that I can solve this on my own next time?

C++

Worked fine on an online compiler. Try changing the name of your constructor parameter. Though on checking your functions you would find following issues (read comments):

  1. Learn the language
  2. Learn its programming practices
  3. Keep the code readable
  4. A few blank lines to group similar content do not have any issue on performance
  5. Create a program to solve a problem
  6. On successful creation, try to make it more readable
  7. Parse your code manually and remove or modify commands for optimization
  8. Keep the code readable
  9. Practice to get comfortable in creating programs
  10. Practice and understand what is happening in the code

Try to solve the mentioned problems on your own. I used an online compiler (as my drive is corrupt) so it took a while. Though I have modified your code to make it work, I will not paste the code here before you try to solve it. Try reading the documentation or Stack Overflow or other forums to get your programming query resolved.