function template has already been defined & signed/unsigned mismatch

I implemented an cpp file however it gives C4018 - signed/unsigned mismatch and C2995 - function template has already been defined error. Is there any solution of these?

Here is the error list of my cpp:

(178): warning C4018: '<' : signed/unsigned mismatch
(64): warning C4018: '>' : signed/unsigned mismatch
(83): warning C4018: '<' : signed/unsigned mismatch
(178): warning C4018: '<' : signed/unsigned mismatch
(48): error C2995: 'HashTable<HashedObj>::HashTable(const HashedObj &,int)' : function template has already been defined
(66): error C2995: 'void HashTable<HashedObj>::insert(const HashedObj &)' : function template has already been defined
(32) : see declaration of 'HashTable<HashedObj>::insert'
(86): error C2995: 'void HashTable<HashedObj>::rehash(void)' : function template has already been defined
(54) : see declaration of 'HashTable<HashedObj>::rehash'
(107): error C2995: 'int HashTable<HashedObj>::findPos(const HashedObj &) const' : function template has already been defined
(53) : see declaration of 'HashTable<HashedObj>::findPos'
(119): error C2995: 'void HashTable<HashedObj>::remove(const HashedObj &)' : function template has already been defined
(33) : see declaration of 'HashTable<HashedObj>::remove'
(133): error C2995: 'const HashedObj &HashTable<HashedObj>::find(const HashedObj &) const' : function template has already been defined
(29) : see declaration of 'HashTable<HashedObj>::find'
(144): error C2995: 'void HashTable<HashedObj>::makeEmpty(void)' : function template has already been defined
(31) : see declaration of 'HashTable<HashedObj>::makeEmpty'
(159): error C2995: 'const HashTable<HashedObj> &HashTable<HashedObj>::operator =(const HashTable<HashedObj> &)' : function template has already been defined
(35) : see declaration of 'HashTable<HashedObj>::operator ='
(169): error C2995: 'bool HashTable<HashedObj>::isActive(int) const' : function template has already been defined
(52) : see declaration of 'HashTable<HashedObj>::isActive'
(175): error C2084: function 'int hash(const std::string &,int)' already has a body
(178): warning C4018: '<' : signed/unsigned mismatch
(58) : see previous definition of 'hash'


This is the file that I wanted to implement:

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
        
        #include "QuadraticProbing.h"
        #include <iostream>
		using namespace std;


        /**
         * Internal method to test if a positive number is prime.
         * Not an efficient algorithm.
         */
        bool isPrime( int n )
        {
            if( n == 2 || n == 3 )
                return true;

            if( n == 1 || n % 2 == 0 )
                return false;

            for( int i = 3; i * i <= n; i += 2 )
                if( n % i == 0 )
                    return false;

            return true;
        }

        /**
         * Internal method to return a prime number at least as large as n.
         * Assumes n > 0.
         */
        int nextPrime( int n )
        {
            if( n % 2 == 0 )
                n++;

            for( ; !isPrime( n ); n += 2 )
                ;

            return n;
        }

        /**
         * Construct the hash table.
         */
        template <class HashedObj>
        HashTable<HashedObj>::HashTable( const HashedObj & notFound, int size )
          : ITEM_NOT_FOUND( notFound ), array( nextPrime( size ) )
        {
            makeEmpty( );
        }

        /**
         * Insert item x into the hash table. If the item is
         * already present, then do nothing.
         */
        template <class HashedObj>
        void HashTable<HashedObj>::insert( const HashedObj & x )
        {
                // Insert x as active
            int currentPos = findPos( x );
            if( isActive( currentPos ) )
                return;
            array[ currentPos ] = HashEntry( x, ACTIVE );

                // Rehash; see Section 5.5
            if( ++currentSize > array.size( ) / 2 )
                rehash( );
        }

        /**
         * Expand the hash table.
         */
        template <class HashedObj>
        void HashTable<HashedObj>::rehash( )
        {
            vector<HashEntry> oldArray = array;

                // Create new double-sized, empty table
            array.resize( nextPrime( 2 * oldArray.size( ) ) );
            for( int j = 0; j < array.size( ); j++ )
                array[ j ].info = EMPTY;

                // Copy table over
            currentSize = 0;
            for( int i = 0; i < oldArray.size( ); i++ )
                if( oldArray[ i ].info == ACTIVE )
                    insert( oldArray[ i ].element );
        }

        /**
         * Method that performs quadratic probing resolution.
         * Return the position where the search for x terminates.
         */
        template <class HashedObj>
        int HashTable<HashedObj>::findPos( const HashedObj & x ) const
        {
/* 1*/      int collisionNum = 0;
/* 2*/      int currentPos = hash( x, array.size( ) );

/* 3*/      while( array[ currentPos ].info != EMPTY &&
                   array[ currentPos ].element != x )
            {
/* 4*/          currentPos += 2 * ++collisionNum - 1;  // Compute ith probe
/* 5*/          if( currentPos >= array.size( ) )
/* 6*/              currentPos -= array.size( );
            }

/* 7*/      return currentPos;
        }


        /**
         * Remove item x from the hash table.
         */
        template <class HashedObj>
        void HashTable<HashedObj>::remove( const HashedObj & x )
        {
            int currentPos = findPos( x );
            if( isActive( currentPos ) )
                array[ currentPos ].info = DELETED;
        }

        /**
         * Find item x in the hash table.
         * Return the matching item or ITEM_NOT_FOUND if not found
         */
        template <class HashedObj>
        const HashedObj & HashTable<HashedObj>::find( const HashedObj & x ) const
        {
            int currentPos = findPos( x );
            if( isActive( currentPos ) )
                return array[ currentPos ].element;
            else
                return ITEM_NOT_FOUND;
        }

        /**
         * Make the hash table logically empty.
         */
        template <class HashedObj>
        void HashTable<HashedObj>::makeEmpty( )
        {
            currentSize = 0;
            for( int i = 0; i < array.size( ); i++ )
                array[ i ].info = EMPTY;
        }

        /**
         * Deep copy.
         */
        template <class HashedObj>
        const HashTable<HashedObj> & HashTable<HashedObj>::
        operator=( const HashTable<HashedObj> & rhs )
        {
            if( this != &rhs )
            {
                array = rhs.array;
                currentSize = rhs.currentSize;
            }
            return *this;
        }


        /**
         * Return true if currentPos exists and is active.
         */
        template <class HashedObj>
        bool HashTable<HashedObj>::isActive( int currentPos ) const
        {
            return array[ currentPos ].info == ACTIVE;
        }

        /**
         * A hash routine for string objects.
         */
        int hash( const string & key, int tableSize )
        {
            int hashVal = 0;

            for( int i = 0; i < key.length( ); i++ )
                hashVal = 37 * hashVal + key[ i ];

            hashVal %= tableSize;
            if( hashVal < 0 )
                hashVal += tableSize;

            return hashVal;
        }


        /**
         * A hash routine for ints.
         */
        int hash( int key, int tableSize )
        {
            if( key < 0 ) key = -key;
            return key % tableSize;
        }
The signed/unsigned warning often happens in a 'for' loop when the index variable is of a different integer type (often 'int') than the bounds for the 'for' loop (often 'size_t').

The template error can likely be explained in the code which defines the HashTable class template, which is not shown, above. Perhaps, you are redefining things?
Topic archived. No new replies allowed.