没有匹配的函数调用

时间:2012-01-29 05:18:16

标签: c++ constructor

我是C ++的新手并尝试编写HashTable数据结构。 我使用模板将它写成通用的,并且我在其中包含了一个HashEntry对象,以便于对碰撞进行简单的二次探测。 我的代码是:

(在.c文件中#include是以下类定义.H文件):

   HashEntry::HashEntry()
   {
      this->isActive = false;
   }

带有类定义的关联.H文件是:

#include <iostream>
#include <string>
#include "Entry.C" 
using namespace std;

#define Default_Size 50000

class HashEntry;

template <class T> class HashTable
{
private:
  int size;
  int occupied;
  T array[Default_Size];

public:
  HashTable();
  int Size();
  void Add(T t);
  void DebugAdd(T t, int index);
  T* Get(string index);
  /* How do I declare the existence of HashEntry BEFORE here? */
  int FindNextOpen(HashEntry he); // Only works for hash_entry objects!
  int Hash(string str);
  void Rehash();
};

class HashEntry
{
private:
  Entry e;
  bool isActive;

public:
  HashEntry();
  HashEntry(Entry e);
  bool IsActive();
  Entry GetEntry();
};

每当我尝试编译所有内容时,我都会收到上面HashEntry构造函数的错误: “没有用于调用Entry :: Entry()的匹配函数”......“候选人是......”。 我不知道这是什么意思 - 当我尝试包含一个默认的Entry()构造函数(我的第一个解释)时,它会抛出更多的错误。

感谢您的帮助!

更新 - ENTRY.C:

#include "Entry.H"
/* ***Entry Methods*** */
/*
 * Overloaded Entry obejct constructor that provides a string value.
 */
Entry::Entry(string s)
{
  this->value = s;
  this->count = 0;
}

/*
 * Returns the number of times this Entry has been accessed/
 * found.
 */
int Entry::Count()
{ return this->count; }

/*
 * Returns the string value stored in the Entry object.
 */
string Entry::Value()
{ return this->value; }

2 个答案:

答案 0 :(得分:3)

  

带有类定义的关联.H文件是:

#include <iostream>
#include <string>
#include "Entry.C"

哇!永远不要#include标头中的源文件。

您的Entry.C不应该存在。而是在类定义中定义标头中的构造函数:

class HashEntry
{
private:
  Entry e;
  bool isActive;

public:
  HashEntry() : isActive(true) {}
...
}

您未向我们展示的一件事是类Entry的定义。这是你问题的根源之一。当你没有向我们展示造成它的事情时,你很难确定问题。

答案 1 :(得分:0)

我发现了问题。 错误消息显示“Entry :: Entry()”没有匹配的函数调用。因为在任何情况下我实际上都没有创建Entry对象,所以我不知道它是什么意思。 我尝试为类Entry添加一个显式的默认构造函数,然后解决了。

感谢大家的帮助!

相关问题