<分区>
我有一个 HashTable< Customer > 作为另一个类的成员。
HashTable< T > 的构造函数采用 int 值以确定 HashTable 数组的大小。
HashTable(int numItems) { ... } //constructor
下面的声明
HashTable<Customer> customers(10000); //doesn't call constructor???
在 10000 下收到错误“需要类型说明符”。当我删除 10000 时,收到错误“找不到客户的函数定义”。这使我相信编译器将我的对象声明视为函数声明。
当我使用动态分配声明我的哈希表时,
HashTable<Customer> * customers = new HashTable<Customer>(10000); //works
与编译器没有混淆。
为什么动态分配起作用,而另一个不起作用?
编辑:这是具有上述相同问题的最小代码。
#ifndef _BUSINESS_LOGIC
#define _BUSINESS_LOGIC
#include "HashTable.h"
class BusinessLogic
{
public:
BusinessLogic();
~BusinessLogic();
void start();
private:
HashTable<int> * custom = new HashTable<int>(10000); //works
HashTable<int> customers(10000); //error
};
#endif
#ifndef _HASH_TABLE
#define _HASH_TABLE
template<class T>
class HashTable
{
public:
HashTable(int numItems) {
if (numItems <= 0) {
throw std::invalid_argument("Invalid HashTable size");
}
currItems = 0;
//B must be the next prime after 2 * numItems
B = numItems;
}
~HashTable() {
}
private:
int B; //size of itemArray
};
#endif