2016-07-31 87 views
0

我已經在這裏讀過幾個線程,並認爲我已經相應地設置了它。我相信問題在於我使用*,並且我沒有正確設置超載設置,即 - 參數定義不正確。問題是編譯和運行成功,所以我不知道我犯了什麼錯誤。無法在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; 
} 

感謝您的任何見解。

+1

沒有「snippets」。出示您的[MCVE]。 –

+0

我現在正在代碼中做的另一件事是加載數組。我添加了它。謝謝。 – thePetester

+1

這不是完整的代碼,請閱讀上面評論中的鏈接並修復它。重構代碼以生成包含所有代碼的單個文件,顯示所有包含的內容以及諸如「正在使用命名空間標準」這樣的內容。我們應該能夠複製您顯示的代碼,將其粘貼到編輯器中並進行編譯。在那之前試圖回答這是浪費時間。 –

回答

3

在:

cout << (mht); 

的不必要的括號mhtHashingTable<char*> *類型。您提供了operator<<的超負荷,需要HashingTable<T> const&。那些不一樣,所以你的超載不被考慮。

你的意思是:

cout << *mht; 
+0

謝謝。我試過了,但是我得到了一個「無法解析的外部錯誤」,我認爲這意味着編譯器無法找到被調用的函數。 – thePetester

+1

這意味着編譯器無法找到被調用函數的定義。如果它是一個模板,那麼定義應該在標題中。 –

+0

是的,但這是因爲我錯誤地調用它嗎?換句話說,因爲我沒有正確定義它?我有一個頭文件。 – thePetester

0

朋友的聲明是相當無用的,實際上是有害。它與實際的函數體不匹配,所以它不會幫助它找到或給予私人成員額外的訪問權限,並且它將在重載解析期間與定義的操作符競爭。

當朋友確實被選中時,編譯器會發出對從不擁有主體的函數的調用,從而導致無法解析的外部錯誤。

這種類型的朋友聲明聲明瞭一個系列非模板函數在封閉名稱空間中。沒有語法來定義它們。

刪除好友聲明。或者以內聯方式移動主體,在這種情況下,您還應該將第二個參數修復爲常量引用,而不是副本。

當你開始調用你寫的操作符函數體時,你會得到其他錯誤,如ht.size()不存在。不要認爲這些錯誤意味着你對操作符的使用是錯誤的 - 它們實際上意味着你的使用是正確的,編譯器現在正試圖解析操作符函數體中的依賴名稱。

+0

謝謝。我會試試這個。我離開了公司,離開了我的個人筆記本電腦。我將在調試這個休息時工作。 謝謝! – thePetester