I'm experimenting on creating my own static library in code::blocks
everything worked fine, i've compiled the lib, no error came up.
now i've created another project which i linked to the static lib, i think i've set up all the necessary build options etc.. but i am having an error :
C:\Users\samsung\Documents\GCC\codeblocks\array\libarray.a
obj\Debug\main.o:main.cpp:(.text+0x18): undefined reference to `array<int, 50u>::array()'
array.hpp : ( just ignore the doxygen comments, i am just experimenting w/ it :)) )
#ifndef ARRAY_HPP_INCLUDED
#define ARRAY_HPP_INCLUDED
//! @author shadow fiend
//! @version 1.0
/**
* @brief array class template container
*
* 2013 shadow fiend
*/
//////////////////////////////////
#include <cstdlib>
//////////////////////////////////
template<class Type, size_t Size>
class array;
//////////////////////////////////
//
// *-* Unfinished *-*
//
//
template<class Type, size_t Size>
class array
{
public :
/**
* @brief initializes the array with a value of 0 ( zero )
*/
array();
/**
* @brief accesses the value of the element specified
*
* @param[in] the index
* @return the value of the element specified
*/
Type& operator[] ( constint& index );
/**
* @brief returns the size of the array
*
* @return an unsigned integral type which is the size of the array
*/
size_t size () const { return sizeOfArray; }
/**
* @brief returns the size of the array ( in terms of bytes
*
* @return an unsigned integral type which is the size of the array in bytes
*/
size_t size_byte () const { returnsizeof( value ); }
private :
enum { _size = Size };
size_t sizeOfArray;
Type value[ _size ];
};
#endif // ARRAY_HPP_INCLUDED
array.cpp :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include "array.hpp"
#include <cassert>
template<class Type, size_t Size>
array<Type, Size>::array()
{
sizeOfArray = Size;
for( size_t& i : value ) {
i = 0;
}
}
template<class Type, size_t Size>
Type& array<Type, Size>::operator[] ( constint& index )
{
assert( index < sizeOfArray );
return value[ index ];
}
and in my other project w/c uses it :
1 2 3 4 5 6 7 8 9 10 11 12 13
// *-*-* Demo for my template class array *-*-*
#include <iostream>
#include "array.hpp"
usingnamespace std;
int main()
{
array<int, 50> myarray1;
return 0;
}
For template classes, you need the entire definition in the header file. You can't split them between header and cpp files the way you can with normal classes.