why am i getting undefined reference ( code::blocks ) ?

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 :)) )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#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[] ( const int& 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 { return sizeof( 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[] ( const int& 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"

using namespace 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.
wow, thanks about that :DDD
another thing to mention there is a std::array class so you may want to remove the using namespace std; to remove name collisions.
Topic archived. No new replies allowed.