Problem With Template Functions In a Namespace.

closed account (zb0S216C)
Here is the code:
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
namespace FunctionNamespace
{
    //--------------------------------------------------------\\
    // GetElementCount( ).                                    \\
    // Returns the amount of elements within the given array. \\
    //--------------------------------------------------------\\
    template < typename ArrayType >
    size_t GetElementCount( const ArrayType p_Array[ ] = NULL )
    {
        // Make sure that the array is valid( not NULL ).
        if( p_Array != NULL )
        {
            // Compute the amount of elements in the array.
            size_t t_stElementCount( sizeof( p_Array ) / sizeof( ArrayType ) );

            // Return the amount of elements in the array.
            return t_stElementCount;
        }

        // if the array is invalid.
        else if( p_Array == NULL )
        {
            // Return zero.
            return 0;
        }
    }

    //---------------------------------------------------------------------------\\
    // CompareValues( ).                                                         \\
    // Compares the left value with the right value. Returns true if they match. \\
    //---------------------------------------------------------------------------\\
    template < typename VariableType >
    bool CompareValues( const VariableType &p_CmpLeft, const VariableType &p_CmpRight )
    {
        // Compare the two and return the result.
        return( ( p_CmpLeft == p_CmpRight ) ? true : false );
    }
};


Here are the errors I'm getting:

error: ISO C++ forbids declaration of 'ArrayType' with no type
error: expected ',' or '...' before 'p_Array'

In function 'size_t FunctionNamespace::GetElementCount(int)':
error: 'p_Array' was not declared in this scope
error: ISO C++ forbids declaration of 'VariableType' with no type
error: expected ',' or '...' before '&' token

In function 'bool FunctionNamespace::CompareValues(int)':
error: 'p_CmpLeft' was not declared in this scope
error: 'p_CmpRight' was not declared in this scope

What am I doing wrong?
Last edited on
Okay, this is the definition of the word "screwy". All I have to do to fix the errors is move them out of the namespace (which should not end with a semicolon), and rewrite the namespace around the moved code. I'm guessing you have some hidden characters somewhere.

EDIT: Idiot me, of course, those comments. You can't use \ in comments like that. '\' has a special meaning: it tells the preprocessor to ignore a newline right after it, and I'm guessing there was some confusion as to when your comment actually ended. All you need to do is add an extra line under your \\ed comments.

EDIT1876: Done editing now.

-Albatross
Last edited on
closed account (zb0S216C)
¬_¬ I'm a tool. Thanks Albatross.
A much better GetElementCount:

1
2
3
template< typename T, size_t N >
size_t GetElementCount( T (&)[ N ] )
{ return N; }


closed account (zb0S216C)
I've seen that function deceleration before. How does it work exactly?
All it says is that GetElementCount is a function that takes a fixed-length array as its parameter
and returns the number of elements.

So

1
2
int array[ 10 ];
std::cout << GetElementCount( array );


Outputs 10, since we instantiated GetElementCount on an int[10].
closed account (zb0S216C)
Thanks jsmith. I'll use your version of GetElementCount( ) instead :)
Topic archived. No new replies allowed.