#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) ===|
編輯:鏈表,數組和哈希表所有編譯和無主建的罰款。 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 ];
}
大部分的代碼是由我們的教科書提供的,我只是需要它運行,請幫助!
在你的眼睛前面有'LinkedList.h'和'Array.h'和'HashTable.h',你找不到任何錯誤。你如何期待*我們*發現他們有什麼問題? –
我主要詢問main.cpp文件錯誤,抱歉不清楚。 – Cyberhawk94
這些錯誤是由較早的錯誤引起的,因此毫無意義。 –