无法在C++中调用重载<<运算符

Can't call overload << operator in C++

本文关键字:lt 运算符 重载 调用 C++      更新时间:2023-10-16

我已经阅读了这里的几个线程,并认为我已经相应地设置了它。我相信问题是我正在使用一个*,并且我没有过载<lt;以某种方式正确设置,即参数定义不正确。问题是编译和运行成功,所以我不知道我在哪里犯了错误。

如果以前有人回答过这个问题,我真的很抱歉,但我找不到。

#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;
} 

感谢您的真知灼见。

In:

cout << (mht);

不需要加括号的mht属于HashingTable<char*> *类型。您提供了占用HashingTable<T> const&operator<<过载。这些都不一样,所以你的过载不会被考虑在内。

你的意思是:

cout << *mht;

你的朋友声明是无用的,实际上是有害的。它与实际的函数体不匹配,因此它不会帮助找到它,也不会为私有成员提供额外的访问权限,而且它将在重载解决过程中与定义的运算符竞争。

当朋友被选中时,编译器会发出对一个从未拥有主体的函数的调用,从而导致未解决的外部错误。

这种类型的友元声明在封闭命名空间中声明了一系列非模板函数。没有语法可以用来定义它们。

删除好友声明。或者内联移动主体,在这种情况下,还应该将第二个参数固定为常量引用,而不是副本。

当您开始调用您编写的运算符函数体时,您将得到其他错误,例如ht.size()不存在。不要认为这些错误意味着你对运算符的使用是错误的——它们实际上意味着你的使用是正确的,编译器现在正试图解析运算符函数体中的依赖名称。