Undefined Reference to Array members

Started by
1 comment, last by fireshadow4126 11 years, 11 months ago
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?

Thanks in advance!
"Welcome to the desert of the real." -- Morpheus, The Matrix.
Advertisement
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.
Wow I never knew that. Thanks a lot!
"Welcome to the desert of the real." -- Morpheus, The Matrix.

This topic is closed to new replies.

Advertisement