我写了一个哈希表类和表头,但我不能在 main 上构建它。它给出了“没有合适的默认构造函数可用”。这是什么原因?
我的标题中的构造函数:HashTable.h
explicit HashTable( const HashedObj & notFound, int size = 101 );
HashTable( const HashTable & rhs )
: ITEM_NOT_FOUND( rhs.ITEM_NOT_FOUND ),
array( rhs.array ), currentSize( rhs.currentSize ) { }
我的 cpp 中的构造函数:HashTable.cpp
HashTable<HashedObj>::HashTable( const HashedObj & notFound, int size )
: ITEM_NOT_FOUND( notFound ), array( nextPrime( size ) )
{
makeEmpty( );
}
我正在尝试在我的 main 中执行以下代码:
HashTable <int> * hash = new HashTable<int>();
最佳答案
HashTable <int> * hash = new HashTable<int>();
您已经定义了一个接受参数的构造函数,但在这里您没有将参数传递给构造函数。相反,您使用的是您的类中不存在的无参数(默认)构造函数。
请注意,如果您在类中定义了一个构造函数(它接受参数),那么编译器将不会为您生成默认构造函数。您必须自己定义它。
关于c++ - 创建构造函数时没有合适的默认构造函数可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13660080/