我已經在這裏讀過幾個線程,並認爲我已經相應地設置了它。我相信問題在於我使用*,並且我沒有正確設置超載設置,即 - 參數定義不正確。問題是編譯和運行成功,所以我不知道我犯了什麼錯誤。無法在C++中調用超載<<運算符
我真的很抱歉,如果這之前已被回答,但我找不到它。
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <list>
#include <exception>
using namespace std; //Allows us to not prepend std:: to many things.
template<class t>
class HashingTable
{
public:
HashingTable();
HashingTable(int size);
void insert(const char *x);
private:
int x = 0;
vector<list<const char *>> hashedLists;
int currentSize;
int hash(const char * key);
friend std::ostream& operator << (std::ostream & os, const HashingTable<t>);
};
template<class t> std::ostream& operator<< (ostream & os, const HashingTable<t> &ht)
{
const int listSize = ht.size();
for (unsigned int i = 0; i < listSize; i++)
{
list<const char *> searchList = ht[i];
for (std::list<const char *>::const_iterator si = std::next(searchList.begin(), listSizeLimit); si != searchList.end(); ++si) //for each value in hashed list.
cout << *si << " ";
}
return os;
};
template<class t>
int HashingTable<t>::hash(const char * key) {
return key[0] - 'A';
};
template<class t>
HashingTable<t>::HashingTable(int size)
{
hashedLists.resize(size);
};
template<class t>
HashingTable<t>::HashingTable()
{
hashedLists.resize(0);
};
template<class t>
void HashingTable<t>::insert(const char *x) {
//string tempStr(x);
unsigned int hashVal = hash(x);
if (hashedLists.size() < (hashVal + 1)) //if the number of lists in the current vector is less than the resize value then...
hashedLists.resize(hashVal + 1); //resize the hashedLists vector
list<const char *> iList = hashedLists[hashVal];
if (std::find(iList.begin(), iList.end(), x) == iList.end())
{
iList.push_back(x);
hashedLists[hashVal] = iList;
}
currentSize++;
};
int main() /* A sample main program */
{
HashingTable<char*>* mht;
char* Names[25] = { "AB", "AC", "AE", "AZ",
"BA","BM", "BJ", "BZ",
"CA", "CX", "CZ", "CZZ",
"EJ", "EP", "EF", "ES",
"QW", "QE", "QR", "QD",
"SA", "SD", "SF", "SS", "SJ" };
int i;
mht = new HashingTable<char*>(0);
for (i = 0; i < 25; i++)
(*mht).insert(Names[i]);
cout << "Printing the hash table after inserting...." << endl;
cout << *mht;
cout << endl;
return 0;
}
感謝您的任何見解。
沒有「snippets」。出示您的[MCVE]。 –
我現在正在代碼中做的另一件事是加載數組。我添加了它。謝謝。 – thePetester
這不是完整的代碼,請閱讀上面評論中的鏈接並修復它。重構代碼以生成包含所有代碼的單個文件,顯示所有包含的內容以及諸如「正在使用命名空間標準」這樣的內容。我們應該能夠複製您顯示的代碼,將其粘貼到編輯器中並進行編譯。在那之前試圖回答這是浪費時間。 –