0

I'm trying to write an ArrayList class in c++, but run into the linking problem with templates as described in many places. I don't want to copy all my code into the header file. That would just be ugly. And the vector class probably didn't do that either right?

So how would I include my ArrayList implementation to any cpp file?

Here is my header File:

//ArrayList.h
#include <stdlib.h>

template <typename T> class ArrayList{
    public:
        ArrayList<T>(){
            array = (T*)calloc(sizeof(T), 1);
            length = 1;
            elements = 0;
        }

        T get(int i);
        void set(int i, T el);
        void add(T el);
        int expand(size_t n);
        void remove(int i);
        int size();
        int reserved();
        void fix_size();

    private:
        void copyAll(T arr1[], T arr2[], int len);
        T *array;
        int length, elements;
};

And here is part of my .cpp file:

#include <stdlib.h>
#include <stdio.h>
#include "ArrayList.h"

template <typename T> T ArrayList<T>::get(int i){
    if (i > this->elements || i < 0) return (T)0;
    return this->array[i];
}

template <typename T> int ArrayList<T>::size(){
    return elements;
}

template <typename T> int ArrayList<T>::reserved(){
    return length;
}

template <typename T> int ArrayList<T>::expand(size_t n){
    // implementation
}

template <typename T> void ArrayList<T>::set(int i, T el){
    // implementation
}

template <typename T> void ArrayList<T>::add(T el){
    // implementation
}

template <typename T> void ArrayList<T>::remove(int i){
    // implementation
}

template <typename T> void ArrayList<T>::copyAll(T arr1[], T arr2[], int len){
    // implementation
}
template <typename T> void ArrayList<T>::fix_size(){
    // implementation
}
Poehli
  • 307
  • 4
  • 16
  • 1
    If you want "prettiness", then why not use one of the many existing container classes instead of trying to write your own? Also, your class is seriously broken if `T` is a non-POD type. – PaulMcKenzie Mar 18 '15 at 23:20
  • simply because I like to learn more about it ;) I never used templates before, so what exactly is broken? – Poehli Mar 18 '15 at 23:24
  • You're trying to run a marathon before learning to walk. There are several issues with the code, "ugliness" or "prettiness" not withstanding. You failed to implement the "rule of 3", your class doesn't work with types such as `std::string` or any non-POD types, usage of `calloc` (which is related to the non-POD issue), etc. I suggest you look at successful implementations (not the STL ones since they are very terse, but ones coded by experienced programmers) and learn from that. Trying to do this by yourself just willy-nilly never works, believe me. – PaulMcKenzie Mar 18 '15 at 23:26
  • okay I get that, but what are you referring to, when you say "coded by experienced programmers"? I don't know where to get such code. – Poehli Mar 18 '15 at 23:35
  • There are plenty of examples right here on SO, where the question is similar to yours, and extensive answers given. – PaulMcKenzie Mar 18 '15 at 23:54
  • 1
    You can start here. This should be, more or less, a skeleton class that implements the functions that are required for copying, assignment, destruction, and respects that `T` may be a non-POD type: http://ideone.com/IstTwE – PaulMcKenzie Mar 19 '15 at 00:13
  • Thank you, this seems to be a good start! – Poehli Mar 19 '15 at 11:16

0 Answers0