I am trying to implement my own vector data structure. The code works fine, but when I write my unit tests, there is a linkage error where inside my unit test, the code is not getting linked
Upon encountering this error, I tried accessing a public data member of the class. I also tried accessing the member function which was defined in the header file instead of the source file. These two methods seem to work successfully.
CODE SNIPPET FROM THE UNIT TEST
GIVEN("A vector with some items") {
MyVector<int> v(5);
REQUIRE(v.capacity_ == 5); //works
REQUIRE_NOTHROW(v.test()); //DOESN'T WORK
REQUIRE(v.size() == 5); //DOESN't WORK
}
}
CODE SIPPET FROM THE SOURCE FILE:
/***
* Size of MyVec
* @tparam T
* @return Type int
*/
template<class T>
int MyVector<T>::size() {
return this->size_;
}
// Sample test function
template <class T>
void MyVector<T>::test() {
return;
}
/tmp/ccgR8GPW.o: In function `____C_A_T_C_H____T_E_S_T____0()':
tests_vector_impl.cpp:(.text+0x25ce2): undefined reference to MyVector<int>::test()
tests_vector_impl.cpp:(.text+0x25dbc): undefined reference to `MyVector<int>::size()'
collect2: error: ld returned 1 exit status```