[英]Use a template with cstring type as a data member in a non-template class
It's a kind of complicated question. 这是一个复杂的问题。
class Stats {
public:
Stats();
// some functions
QuadraticHashTable<char[4]> teamHash;
};
Due to the requirements, I have to use the template QuadraticHashTable to store c style string and I have to define a QuadraticHashTable type data member in the class Stats, which is not a template. 由于要求,我必须使用模板QuadraticHashTable来存储c样式字符串,并且必须在Stats类(不是模板)中定义QuadraticHashTable类型的数据成员。
Anyone can tell me what's wrong with my declaration? 有人可以告诉我我的声明有什么问题吗? The compiler keeps telling me that
编译器不断告诉我
"no matching function for call to 'QuadraticHashTable::QuadraticHashTable()' " “没有匹配的函数可以调用'QuadraticHashTable :: QuadraticHashTable()'”
BTW, I haven't initialize the data member in constructor for Stat 顺便说一句,我还没有初始化Stat的构造函数中的数据成员
Thank you. 谢谢。
code for QuadraticHashTable QuadraticHashTable的代码
template <class HashedObj>
class QuadraticHashTable
{
public:
explicit QuadraticHashTable( const HashedObj & notFound, int size = 101 );
QuadraticHashTable( const QuadraticHashTable & rhs )
: array( rhs.array), ITEM_NOT_FOUND( rhs.ITEM_NOT_FOUND ),
currentSize( rhs.currentSize ) { }
const HashedObj & find( const HashedObj & x ) const;
void makeEmpty( );
void insert( const HashedObj & x );
void remove( const HashedObj & x );
const QuadraticHashTable & operator=( const QuadraticHashTable & rhs );
enum EntryType { ACTIVE, EMPTY, DELETED };
/////////////////////////////////////////////////////
friend class Stats;
/////////////////////////////////////////////////////
private:
struct HashEntry
{
HashedObj element;
EntryType info;
HashEntry( const HashedObj & e = HashedObj( ), EntryType i = EMPTY )
: element( e ), info( i ) { }
};
vector<HashEntry> array;
int currentSize;
const HashedObj ITEM_NOT_FOUND;
bool isPrime( int n ) const;
int nextPrime( int n ) const;
bool isActive( int currentPos ) const;
int findPos( const HashedObj & x ) const;
int hash( const string & key, int tableSize ) const;
int hash( int key, int tableSize ) const;
void rehash( );
};
That class has no default constructor. 该类没有默认构造函数。 Whenever your class has a member that has no default constructor you must explicitly call a non default constructor when you construct your object.
只要您的类中的成员没有默认构造函数,则在构造对象时必须显式调用非默认构造函数。 If the member has a default (read parameterless) constructor it's called by default.
如果成员具有默认(读无参数)构造函数,则默认情况下调用它。
For example the following recreates your problem 例如,以下内容重现了您的问题
template <class T>
class Member
{
public:
Member(T obj)
:m_obj(obj)
{
}
private:
T m_obj;
};
class Owner
{
public:
Owner()
{
}
private:
Member<int> m_member;
};
In VS I get a slightly clearer error message Error 'Member' : no appropriate default constructor available , but its the same problem the compiler doesn't know how to build a member in this context because you haven't told it how to construct it.
在VS中,我得到了一条更清晰的错误消息错误'Member':没有合适的默认构造函数 ,但是与编译器不知道如何在此上下文中构造成员的问题相同,因为您没有告诉它如何构造它。
If I change the Owner class to call the appropriate constructor during initialization the compiler now knows how to construct your object. 如果我在初始化期间更改Owner类以调用适当的构造函数,则编译器现在知道如何构造对象。
class Owner
{
public:
Owner()
:m_member(4)
{
}
private:
Member<int> m_member;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.