C++ 非常令人费解的逻辑错误 问题
你好。几个小时以来,我一直在努力解决我认为是逻辑错误的问题。我试图用链表链接创建一个C++中的哈希表。当尝试从哈希表中检索项时,我经常会得到一个错误,即该项不包含在表中,因此无法检索。我已经盯着这个代码看了好几个小时了,我真的需要一些新鲜的眼睛。有人能发现这里出了什么问题吗?对于我提供的大量代码,我深表歉意,但我真的很难找出问题所在 注:我对C++非常陌生,所以请对我说得轻松点!)p> 谢谢你的任何意见C++ 非常令人费解的逻辑错误 问题,c++,linked-list,logic,hashtable,semantics,C++,Linked List,Logic,Hashtable,Semantics,你好。几个小时以来,我一直在努力解决我认为是逻辑错误的问题。我试图用链表链接创建一个C++中的哈希表。当尝试从哈希表中检索项时,我经常会得到一个错误,即该项不包含在表中,因此无法检索。我已经盯着这个代码看了好几个小时了,我真的需要一些新鲜的眼睛。有人能发现这里出了什么问题吗?对于我提供的大量代码,我深表歉意,但我真的很难找出问题所在 注:我对C++非常陌生,所以请对我说得轻松点!)p> 谢谢你的任何意见 散列表 标题 #include "LinkedList.cpp" #include <
散列表 标题
#include "LinkedList.cpp"
#include <iostream>
//#include <string>
using namespace std;
template <typename E>
class HashTable
{
private:
struct HashElement
{
E data;
string key;
friend bool operator>(HashElement lhs, HashElement rhs)
{
if (lhs.key > rhs.key) return true;
else return false;
}
};
const static int CAPACITY = 75;
LinkedList<HashElement> table[CAPACITY];
int totalElements;
int hash(string);
public:
HashTable();
bool contains(string);
void put(string, E);
E get(string);
friend ostream& operator<<(ostream& stream, const HashTable<E> table);
};
#include <iostream>
using namespace std;
#if !defined (LINKEDLIST_H)
#define LINKEDLIST_H
template<typename T>
class LinkedList
{
private:
struct Node
{
T data;
Node* nptr;
};
Node *head, *iter;
int size;
public:
LinkedList ();
LinkedList (const LinkedList<T> &);
~LinkedList ();
const LinkedList& operator=(const LinkedList<T> &);
bool isEmpty (void) const;
void insertItem(T); //add item in sorted order
bool contains(T item);
int getSize();
bool hasNext();
T getNext();
void resetIter();
friend ostream& operator<<(ostream& stream, const LinkedList<T> list)
{
Node *temp;
temp = list.head;
while(temp->nptr != NULL)
{
stream << temp->data << "\n";
temp = temp->nptr;
}
stream << temp->data << endl;
return stream;
}
};
#endif
我总是得到字符串的I=0
。根据文档,如果输入不是数字,则结果未定义
希望这对您需要改进的地方有所帮助。请仅在此处发布相关部分代码。看起来您的svn中有一个完整的转储。感谢您的回复。散列函数最终成为了主要问题(在许多其他小问题中),几个小时后,我把它全部解决了。考虑到有多少代码需要通读,我非常感谢您的回复。
#include <iostream>
using namespace std;
#if !defined (LINKEDLIST_H)
#define LINKEDLIST_H
template<typename T>
class LinkedList
{
private:
struct Node
{
T data;
Node* nptr;
};
Node *head, *iter;
int size;
public:
LinkedList ();
LinkedList (const LinkedList<T> &);
~LinkedList ();
const LinkedList& operator=(const LinkedList<T> &);
bool isEmpty (void) const;
void insertItem(T); //add item in sorted order
bool contains(T item);
int getSize();
bool hasNext();
T getNext();
void resetIter();
friend ostream& operator<<(ostream& stream, const LinkedList<T> list)
{
Node *temp;
temp = list.head;
while(temp->nptr != NULL)
{
stream << temp->data << "\n";
temp = temp->nptr;
}
stream << temp->data << endl;
return stream;
}
};
#endif
#include "LinkedList.h"
template<typename T>
LinkedList<T>::LinkedList()
{
head = NULL;
size = 0;
iter = head;
}
template<typename T>
LinkedList<T>::LinkedList (const LinkedList &rhs)
{
Node *tmpPtr1, *tmpPtr2;
tmpPtr1 = head;
tmpPtr2 = rhs.head;
size = rhs.size;
if (tmpPtr1 != tmpPtr2)
{
while (tmpPtr1 != NULL)
{
head = head->nptr;
delete tmpPtr1;
tmpPtr1 = head;
}
tmpPtr1 = head;
while (tmpPtr2 != NULL)
{
insertItem(tmpPtr2->data);
tmpPtr2 = tmpPtr2->nptr;
}
}
}
template<typename T>
const LinkedList<T>& LinkedList<T>::operator= (const LinkedList &rhs)
{
Node *tmpPtr1, *tmpPtr2;
tmpPtr1 = head;
tmpPtr2 = rhs.head;
size = rhs.size;
if (tmpPtr1 != tmpPtr2)
{
while (tmpPtr1 != NULL)
{
head = head->nptr;
delete tmpPtr1;
tmpPtr1 = head;
}
tmpPtr1 = head;
while (tmpPtr2 != NULL)
{
insertItem(tmpPtr2->data);
tmpPtr2 = tmpPtr2->nptr;
}
}
return *this;
}
template<typename T>
void LinkedList<T>::insertItem(T item)
{
Node *temp, *curr, *prev;
temp = new Node;
temp->data = item;
temp->nptr = NULL;
curr = head;
prev = NULL;
if(head != NULL)
{
while(temp->data > curr->data)
{
if(curr->nptr != NULL)
{
prev = curr;
curr = curr->nptr;
}
else break;
}
if(curr->nptr == NULL)
{
curr->nptr = temp;
}
else
{
if(prev != NULL)
{
temp->nptr = curr;
prev->nptr = temp;
}
else
{
temp->nptr = curr;
head = temp;
}
}
}
else
{
head = temp;
}
size++;
}
template<typename T>
bool LinkedList<T>::contains(T item)
{
Node *temp;
temp = head;
bool result = false;
while(temp->nptr != NULL)
{
if (temp->data == item)
{
result = true;
break;
}
temp = temp->nptr;
}
return result;
}
template<typename T>
bool LinkedList<T>::isEmpty() const
{
return (head == NULL);
}
template<typename T>
int LinkedList<T>::getSize()
{
return size;
}
template<typename T>
bool LinkedList<T>::hasNext()
{
if(iter->nptr != NULL)
return true;
else
return false;
}
template<typename T>
T LinkedList<T>::getNext()
{
if(hasNext())
{
T result;
result = iter->data;
iter = iter->nptr;
return result;
}
else
throw runtime_error("List has no more elements, Use resetIter() to return to beginning\n");
}
template<typename T>
void LinkedList<T>::resetIter()
{
iter = head;
}
template<typename T>
LinkedList<T>::~LinkedList()
{
Node *tmpPtr = head;
while (tmpPtr != NULL)
{
head = tmpPtr->nptr;
delete tmpPtr;
tmpPtr = head;
}
}
#include <iostream>
//#include "LinkedList.cpp"
#include "HTTPlog.cpp"
#include "HashTable.cpp"
using namespace std;
int main()
{
/* HASHTABLE TEST */
HashTable<HTTPlog> table;
HTTPlog names[6];
names[0] = HTTPlog("Marcus", "Kre", 8, 22, 94);
names[1] = HTTPlog("Devon", "Fla", 10, 6, 9);
names[2] = HTTPlog("Larry", "Kre", 2, 1, 63);
names[3] = HTTPlog("Mike", "Fer", 35, 32, 5);
names[4] = HTTPlog("Mdkq", "Bir", 8, 3, 23);
names[5] = HTTPlog("Tess", "Thi", 6, 6, 6);
for(HTTPlog l: names)
{
table.put(l.getResourceName(), l);
}
cout << table.contains("Marcus") << table.contains("Devon") << table.contains("Larry") << table.contains("Mike") << table.contains("JSDlfjsl") << endl;
cout << "Successfully checked contains" << endl;
cout << table.get("Marcus") << table.get("Mdkq") << table.get("Test") << endl;
cout << "Sccessfully got" << endl;
return 0;
}
11110
Successfully checked contains
Name: Marcus
Date: Kre
Status Code: 8
Total Bytes: 22
Request Count: 94
Name: Mdkq
Date: Bir
Status Code: 8
Total Bytes: 3
Request Count: 23
libc++abi.dylib: terminating with uncaught exception of type std::runtime_error: The item is not contained in the HashTable
Abort trap: 6
names[5] = HTTPlog("Tess", "Thi", 6, 6, 6);
...
cout << table.get("Marcus") << table.get("Mdkq") << table.get("Test") << endl;
int i = atoi(key.c_str());