# Undefined Reference to Array members

This topic is 2189 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello,

I'm programming a game engine in C++, mostly for educational purposes. I have a custom generic array class (mostly so that I can add better handling of dynamic allocation later) and everything compiles fine, but when I try to link I get this:
 libsurvive/bin/debug/libsurvive.so: undefined reference to Survive::System::Array<Survive::Math::Vector3>::~Array()' libsurvive/bin/debug/libsurvive.so: undefined reference to Survive::System::Array<Survive::Math::Vector2>::~Array()' libsurvive/bin/debug/libsurvive.so: undefined reference to Survive::System::Array<Survive::Math::Vector3>::Array()' libsurvive/bin/debug/libsurvive.so: undefined reference to Survive::System::Array<Survive::Math::Vector3>::resize(unsigned long)' libsurvive/bin/debug/libsurvive.so: undefined reference to Survive::System::Array<Survive::Math::Vector3>::size() const' libsurvive/bin/debug/libsurvive.so: undefined reference to Survive::System::Array<Survive::Math::Vector2>::Array()' libsurvive/bin/debug/libsurvive.so: undefined reference to Survive::System::Array<Survive::Math::Vector2>::resize(unsigned long)' libsurvive/bin/debug/libsurvive.so: undefined reference to Survive::System::Array<Survive::Math::Vector3>::operator[](unsigned long)' libsurvive/bin/debug/libsurvive.so: undefined reference to Survive::System::Array<Survive::Math::Vector3>::clear()' libsurvive/bin/debug/libsurvive.so: undefined reference to Survive::System::Array<Survive::Math::Vector2>::clear()' libsurvive/bin/debug/libsurvive.so: undefined reference to Survive::System::Array<Survive::Math::Vector2>::size() const' 

what is interesting about this is that the only errors seem to be with constructors/destructors and with functions that use 'size_t.' Anyway, here's the definition of the array class:
 #include <stddef.h> namespace Survive { namespace System { template<typename Type> class Array { public: Array(); Array(size_t size); ~Array(); void add(Type item); void clear(); size_t size() const; Type &operator[](size_t i); void resize(size_t newSize); private: Type *pointer; size_t capacity; size_t numElements; }; } } `

I'm using g++ and makefiles generated by premake.

Anyone have any idea on how to fix this problem?

##### Share on other sites
Looks like the typical error where you try to define the templates in a separate file. Declaration and definition cannot be separate; the full declaration of everything has to be visible at every point where the template is instantiated. Put the code for all your functions in the header file instead of in a separate file. Edited by Brother Bob

##### Share on other sites
Wow I never knew that. Thanks a lot!

• 10
• 17
• 9
• 13
• 41