无法在C++中调用重载<<运算符
Can't call overload << operator in C++
我已经阅读了这里的几个线程,并认为我已经相应地设置了它。我相信问题是我正在使用一个*,并且我没有过载<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()
不存在。不要认为这些错误意味着你对运算符的使用是错误的——它们实际上意味着你的使用是正确的,编译器现在正试图解析运算符函数体中的依赖名称。
相关文章:
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 如何显式调用运算符<<
- 为什么COUT在朋友函数中不起作用,该功能超载了操作员&lt;&lt;这是一个iStream运算符
- C++运算符<<调用::ostream而不是std::osttream
- BOOST ::变体无法解决运算符&lt;&lt;对于STD :: Ostream
- 过载输出<<用于类的运算符,以打印其中的元组
- C++ostream:没有运算符匹配<<&应在'&'代币
- 重载运算符<<:此运算符函数的参数太多
- C++继承运算符<<
- 重载运算符<<用于模板类.即使使用好友关键字也无法获得私人会员
- 如何过载<<用于YAML::Emitter的运算符,以序列化包含另一个自定义类的向量的自定义类
- 为什么字符串流运算符<<擦除原始值
- 关于使用运算符<<为新手提供C++中的模板
- 我已经完成了<<运算符重载,但它'It’不起作用
- 重载运算符<<输出地址而不是数据成员
- 错误:没有匹配'运算符<<"在'std::cout
- 重载运算符<<用于ostream语法
- 当运算符<存在时,为什么要定义 LT?
- log4cxx访问异常,使用<<运算符和宏
- 重载<<运算符错误C2804:二进制'运算符<<'参数太多