为什么编译器认为我的对象声明是函数声明?
Why does the compiler think my object declaration is a function declaration?
我有一个 HashTable< Customer > 作为另一个 class 中的成员。
HashTable< T > 的构造函数采用 int 值以确定 HashTable 数组的大小。
HashTable(int numItems) { ... } //constructor
下面声明
HashTable<Customer> customers(10000); //doesn't call constructor???
在 10000 下收到错误 "expected a type specifier"。当我删除 10000 时,收到错误 "Function definition for customers not found." 这让我相信编译器将我的对象声明视为函数声明。
当我使用动态分配声明我的哈希表时,
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
您不能以这种方式为成员变量提供默认成员初始值设定项。你可以选择
HashTable<Customer> customers = HashTable<Customer>(1000);
或
HashTable<Customer> customers {1000};
或者直接在构造函数中
BusinessLogic::BusinessLogic(): customers(1000) { }
在 class 定义中直接为 class 成员提供初始值设定项时,不允许使用 ()
初始值设定项语法。它需要 =
语法或 {}
封闭的初始值设定项。在你的情况下,它可以是
HashTable<int> customers{10000};
或
HashTable<int> customers = 10000;
或者,如果您愿意
HashTable<int> customers = { 10000 };
最后两个版本之所以有效,是因为您的 HashTable
专业化提供了适当的转换构造函数。如果该构造函数被声明为 explicit
,则必须使用
HashTable<int> customers = HashTable<int>(10000); // or `= HashTable<int>{10000}`
代替第二个 and/or 第三个变体。
您尝试使用的初始化程序实际上正式称为 brace-or-equal-initializer。该名称暗示了语法的正确变体。
我有一个 HashTable< Customer > 作为另一个 class 中的成员。
HashTable< T > 的构造函数采用 int 值以确定 HashTable 数组的大小。
HashTable(int numItems) { ... } //constructor
下面声明
HashTable<Customer> customers(10000); //doesn't call constructor???
在 10000 下收到错误 "expected a type specifier"。当我删除 10000 时,收到错误 "Function definition for customers not found." 这让我相信编译器将我的对象声明视为函数声明。
当我使用动态分配声明我的哈希表时,
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
您不能以这种方式为成员变量提供默认成员初始值设定项。你可以选择
HashTable<Customer> customers = HashTable<Customer>(1000);
或
HashTable<Customer> customers {1000};
或者直接在构造函数中
BusinessLogic::BusinessLogic(): customers(1000) { }
在 class 定义中直接为 class 成员提供初始值设定项时,不允许使用 ()
初始值设定项语法。它需要 =
语法或 {}
封闭的初始值设定项。在你的情况下,它可以是
HashTable<int> customers{10000};
或
HashTable<int> customers = 10000;
或者,如果您愿意
HashTable<int> customers = { 10000 };
最后两个版本之所以有效,是因为您的 HashTable
专业化提供了适当的转换构造函数。如果该构造函数被声明为 explicit
,则必须使用
HashTable<int> customers = HashTable<int>(10000); // or `= HashTable<int>{10000}`
代替第二个 and/or 第三个变体。
您尝试使用的初始化程序实际上正式称为 brace-or-equal-initializer。该名称暗示了语法的正确变体。