Template Class - Method Definition Problem

closed account (zb0S216C)
Firstly, here is my header:

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
    namespace MBB
    {
        template < typename BLOCK_TYPE >
        struct MBB_Memory_bin
        {
            MBB_Memory_bin( void );
            MBB_Memory_bin( Identifier New_ID, Sizeof_block Element_count );
            ~MBB_Memory_bin( void );

            struct Memory_bin_element
            {
                Identifier Element_ID;
                Bit_flags Element_flags;
                BLOCK_TYPE *Element_block;
            };

            protected:

            struct Bin_info
            {
                short Last_bin_element;    // Last valid index within the bin.
                Identifier Bin_ID;         // The Bin's identifier.
            } Bin_info;

            public:

            // Name: Memory_bin
            // Holds the specified amount of bin elements. The given amount of
            // elements is used as the maximum amount of elements.
            Memory_bin_element *Memory_bin;

            // Name: Attach_element( )
            // Adds a new element to the bin. If successful, the newly allocated
            // element is returned to allow element initialization.
            Memory_bin_element *Attach_element( BLOCK_TYPE Init_obj, Bit_flags New_flags, Identifier New_ID );

            // Name: Detach_element( )
            // Removes the element with the specified identifier within the bin.
            // If no matching identifier is found, no action is taken and false
            // is returned. Else, true is returned on a successful detachment.
            bool Detach_element( Identifier Target_ID );

            // Name: Release_bin( )
            // Deallocates any memory allocated by the bin.
            bool Release_bin( void );

            // Name: Bring_back_element( )
            // Takes the given identifier and compares the given identifier with
            // the identifiers of the elements. If an identifier matches the given
            // identifier, the element is returned.
            Memory_bin_element *Bring_back_element( Identifier Target_ID ) const;
        };
    }

Secondly, this is my source file:

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
58
59
60
61
namespace MBB
{
    template < typename BLOCK_TYPE >
    MBB_Memory_bin < BLOCK_TYPE >::MBB_Memory_bin( void )
    {
        Bin_info.Bin_ID = 0;
        Bin_info.Last_bin_element = -1;
        Memory_bin = 0;
    }

    template < typename BLOCK_TYPE >
    MBB_Memory_bin < BLOCK_TYPE >::MBB_Memory_bin( Identifier New_ID, Sizeof_block Element_count )
    {
        Bin_info.Bin_ID = New_ID;
        switch( ( Element_count > 0 ) ? true : false )
        {
            case true:
                Memory_bin = new MBB_Memory_bin[ Element_count ];
                Bin_info.Last_bin_element = ( Element_count - 1 );
                break;

            case false:
                Memory_bin = 0;
                Bin_info.Last_bin_element = -1;
                Bin_info.Bin_ID = New_ID;
                break;
        }
    }

    template < typename BLOCK_TYPE >
    MBB_Memory_bin < BLOCK_TYPE >::~MBB_Memory_bin( void )
    {
        switch( ( Memory_bin != 0 ) ? true : false )
        {
            case true:
                // Deallocate any memory allocated by the elements.
                for( short Index( 0 ); Index < ( Bin_info.Last_bin_element + 1 ); Index++ )
                {
                    delete Memory_bin[ Index ].Element_block;
                        Memory_bin[ Index ].Element_block = 0;
                    Memory_bin[ Index ].Element_ID = 0;
                    Memory_bin[ Index ].Element_flags = 0;
                }

                delete [ ] Memory_bin;
                    Memory_bin = 0;
                Bin_info.Bin_ID = 0;
                Bin_info.Last_bin_element = -1;
                break;

            case false:
                break;
        }
    }

    // This is the problem child...
    template < typename BLOCK_TYPE >
    MBB_Memory_bin < BLOCK_TYPE >::Memory_bin_element *MBB_Memory_bin < BLOCK_TYPE >::Attach_element( BLOCK_TYPE Init_obj, Bit_flags New_flags, Identifier New_ID )
    {
    }
}

Here's the problem: The last method definition is giving me this error( only error with no warnings ):
expected constructor, destructor, or type conversion before '*' token

Any ideas?
Last edited on
I suppose that you are including your source at the end of the header

1
2
 template < typename BLOCK_TYPE >
typename MBB_Memory_bin < BLOCK_TYPE >::Memory_bin_element* //... 

http://www.cplusplus.com/forum/general/37217/#msg201431
You need to use the typename keyword
1
2
3
4
template < typename BLOCK_TYPE >
    typename MBB_Memory_bin < BLOCK_TYPE >::Memory_bin_element *MBB_Memory_bin < BLOCK_TYPE >::Attach_element( BLOCK_TYPE Init_obj, Bit_flags New_flags, Identifier New_ID )
    {
    }
closed account (zb0S216C)
The typename keyword solved it. One question though, why doesn't the rest of the method definitions require the typename keyword?
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.18
because they are well defined (they are no dependent). Bring_back_element should need it too.
closed account (zb0S216C)
Thanks for the link, Ne555. I'll read upon that now. Also, thanks for the heads up on Bring_back_element( ).
Last edited on
Topic archived. No new replies allowed.