#include <iostream>
#include <string>
#include "HashTable.h"
using namespace std;
struct MyStruct {
string str;
int num;
bool operator ==( const MyStruct & r ) { return str == r.str; }
};
const int SIZE1 = 97;
int hash1( const MyStruct & obj );
int main( )
{
HashTable<MyStruct> ht1( hash1, SIZE1 );
MyStruct myobj;
myobj.str = "elephant";
myobj.num = 25;
ht1.insert( myobj );
MyStruct myobj2;
myobj2.str = "elephant";
ht1.retrieve( myobj2 );
cout << "retrieved from ht1: " << myobj2.num << " for num." << endl;
return 0;
}
int hash1( const MyStruct & obj )
{
int sum = 0;
for ( int i = 0; i < 3 && i < int( obj.str.length( ) ); i++ )
sum += obj.str[ i ];
return sum % SIZE1;
}
这是我的主要功能的代码,我得到了大量的错误,找不到任何错误。
H:\CSC 375\Homework5\LinkedList.h|200|warning: no newline at end of file|
H:\CSC 375\Homework5\Array.h||In constructor `Array<DataType>::Array(int)':|
H:\CSC 375\Homework5\Array.h|46|error: expected primary-expression before "template"|
H:\CSC 375\Homework5\Array.h|46|error: expected `;' before "template"|
每次使用模板重复
H:\CSC 375\Homework5\HashTable.h|6|error: expected primary-expression before "template"|
H:\CSC 375\Homework5\HashTable.h|6|error: expected `;' before "template"|
每次使用模板重复
H:\CSC 375\Homework5\HashTable.h|82|warning: no newline at end of file|
H:\CSC 375\Homework5\main.cpp|17|error: expected primary-expression before "int"|
H:\CSC 375\Homework5\main.cpp|17|error: expected `;' before "int"|
H:\CSC 375\Homework5\main.cpp|38|error: a function-definition is not allowed here before '{' token|
H:\CSC 375\Homework5\main.cpp|38|error: expected `,' or `;' before '{' token|
H:\CSC 375\Homework5\main.cpp|44|warning: no newline at end of file|
H:\CSC 375\Homework5\main.cpp|43|error: expected `}' at end of input|
||=== Build finished: 35 errors, 3 warnings (0 minutes, 0 seconds) ===|
编辑:链表、数组和哈希表都可以在没有 main.cpp 的情况下编译和构建,所以它必须是那个文件中的东西。
双重编辑:文件代码(我在使用模板时将接口和实现放在同一个文件中,链接 .h 和 .cpp 的问题太多)
// HashTable.h
#include "LinkedList.h"
#include "Array.h"
template <class DataType>
class HashTable
{
public:
HashTable( int (*hashf)(const DataType &), int s );
bool Insert( const DataType & newObject );
bool retrieve( DataType & retrieved );
bool Remove( DataType & removed );
bool update( DataType & updateObject );
void makeEmpty( );
private:
Array< LinkedList<DataType> > table;
int (*hashfunction)(const DataType &);
};
// HashTable.cpp
template <class DataType>
HashTable<DataType>::HashTable( int (*hashf)(const DataType &), int s )
: table( s )
{
hashfunc = hashf;
}
template <class DataType>
bool HashTable<DataType>::Insert( const DataType & newObject )
{
int location = hashfunc( newObject );
if ( location < 0 || location >= table.length( ) )
return false;
table[ location ].Insert( newObject );
return true;
}
template <class DataType>
bool HashTable<DataType>::retrieve( DataType & retrieved )
{
int location = hashfunc( retrieved );
if ( location < 0 || location >= table.length( ) )
return false;
if ( !table[ location ].retrieve( retrieved ) )
return false;
return true;
}
template <class DataType>
bool HashTable<DataType>::Remove( DataType & removed )
{
int location = hashfunc( removed );
if ( location < 0 || location >= table.length( ) )
return false;
if ( !table[ location ].Remove( removed ) )
return false;
return true;
}
template <class DataType>
bool HashTable<DataType>::update( DataType & updateObject )
{
int location = hashfunc( updateObject );
if ( location < 0 || location >= table.length( ) )
return false;
if ( !table[location].find( updateObject ) )
return false;
table[location].Replace( updateObject );
return true;
}
template <class DataType>
void HashTable<DataType>::makeEmpty( )
{
for ( int i = 0; i < table.length( ); i++ )
table[ i ].makeEmpty( );
}
.... // 数组.h
#include <string>
using namespace std;
template <class DataType>
class Array
{
public:
Array( int Size );
Array( const Array<DataType> & ap );
~Array( );
Array<DataType> & operator =( const Array<DataType> & right );
inline DataType & operator [ ]( int index );
void changeSize( int newSize );
inline int length( ) const;
string err( ) const;
private:
DataType *elements;
int capacity;
DataType dud;
int errorCode;
inline void deepCopy( const Array<DataType> & original );
};
// Array.cpp
template <class DataType>
Array<DataType>::Array( int Size )
{
if ( Size < 1 ) {
capacity = 1;
errorCode = 1;
}
else {
capacity = Size;
errorCode = 0;
elements = new DataType [capacity];
}
template <class DataType>
Array<DataType>::Array( const Array<DataType> & ap )
{
deepCopy( ap );
}
template <class DataType>
Array<DataType>::~Array( )
{
delete [ ] elements;
}
template <class DataType>
Array<DataType> & Array<DataType>::operator =( const Array<DataType> & right )
{
if ( this == &right )
return *this;
delete [ ] elements;
deepCopy( right );
return *this;
}
template <class DataType>
inline DataType & Array<DataType>::operator [ ]( int index )
{
#ifdef DEBUG_ARRAY
if ( index < 0 || index >= capacity ) {
errorCode |= 2;
return dud;
}
#endif
return elements[ index ];
}
template <class DataType>
void Array<DataType>::changeSize( int newSize )
{
if ( newSize < 1 )
{
errorCode |= 4;
return;
}
DataType *newArray = new DataType [newSize];
int limit = (newSize > capacity)? capacity : newSize;
for ( int i = 0; i < limit; i++ )
newArray[ i ] = elements[ i ];
delete [ ] elements;
elements = newArray;
capacity = newSize;
}
template <class DataType>
inline int Array<DataType>::length( ) const
{
return capacity;
}
template <class DataType>
string Array<DataType>::err( ) const
{
if ( errorCode == 0 )
return "No error.\n";
string errorMessage = "";
if ( errorCode & 1 ) {
errorMessage += "Nonpositive size passed into constructor, so\n";
errorMessage += "the capacity was set to 1 by default.\n";
}
if ( errorCode & 2 )
errorMessage += "Index out of range.\n";
if ( errorCode & 4 ) {
errorMessage += "Nonpositive size passed into changeSize, so\n";
errorMessage += "the size of the array was not changed.\n";
}
return errorMessage;
}
template <class DataType>
inline void Array<DataType>::deepCopy( const Array<DataType> & original )
{
capacity = original.capacity;
errorCode = original.errorCode;
elements = new DataType [capacity];
for ( int i = 0; i < capacity; i++ )
elements[ i ] = original.elements[ i ];
}
大部分代码是我们教科书提供的,我只需要它运行,请帮助!