c++ - 创建构造函数时没有合适的默认构造函数可用

标签 c++ visual-studio-2010 constructor

我写了一个哈希表类和表头,但我不能在 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/

相关文章:

c++ - 纯函数对象的内存结构?

c++ - 使用 al_ustr_newf 复制字符串 Allegro 5

C++模板成员函数错误

mysql - System.Data.SqlClient.SqlException 字符串后未闭合的引号

visual-studio-2010 - 在 Visual Studio 中创建源文件的物理副本

c++ - 除了规定的参数和 "this"指针之外,c++ (g++) 构造函数还以什么顺序获取其他参数?

c++ - 是 *this = Ctor();清除对象状态合法有效?

c++ - C++ 打印除数函数的新功能

vb.net - Visual Studio 2010 调试器不再因错误而停止

android - 是否可以覆盖启动完整的构造函数?