-1

I am attempting to implement some small generic data structures as review and to learn more c++, but am running into problems with getting my basic template class to compile.

Header file:

template <class T>
class ArrayList {
    public: 
        ArrayList(T dataToAdd[]);
};

C++ file:

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

template <class T>
ArrayList<T>::ArrayList(T dataToAdd[]) {
    std::cout << "ArrayList constructor" << std::endl;
}

main.cpp:

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

int main(int argc, const char* argv[]) {

    int data[3] = {1, 2, 3};

    ArrayList<int> testList(data);

    return 0;
} 

I am thinking that the problem has to do with the array being passed into the constructor in main.cpp, as when I removed all parameters (from header, implementation, and actually calling the constructor) the program compiled fine.

The error I am receiving:

/usr/bin/ld: /tmp/cc5ra82s.o: in function `main':
main.cpp:(.text+0x46): undefined reference to `ArrayList<int>::ArrayList(int*)'
collect2: error: ld returned 1 exit status
tjulich
  • 51
  • 6

1 Answers1

1

You have to implement the definition of template classes directly in the header file.

Tom Gebel
  • 744
  • 1
  • 4
  • 13
  • Thank you so much! Very new to C++ and mainly doing this to learn about how to implement templates, so that is certainly a huge help! – tjulich Jan 04 '21 at 02:18
  • Please mark duplicates as duplicates, instead of answering. – ChrisMM Jan 04 '21 at 03:04