c++ - 没有匹配的函数调用

标签 c++ constructor

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

(在 #include 下面的类定义 .H 文件的 .C 文件中):

   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() 构造函数(我的第一个解释)时,它会抛出更多错误。

感谢您的帮助!

更新——条目.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; }

最佳答案

And the associated .H file with the class definitions is:

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

哇哦!永远不要在头文件中#include 源文件。

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

class HashEntry
{
private:
  Entry e;
  bool isActive;

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

您没有向我们展示的一件事是 Entry 类的定义。那是您问题的根源之一。如果您没有向我们展示导致问题的原因,则很难确定您的问题。

关于c++ - 没有匹配的函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9051121/

相关文章:

c++ - 在同一个 CMakeList 文件中导入两个不同的项目

java - 构造函数及其使用方式

c++ - 什么是严格的别名规则?

c++ - 解析一个非常简单的配置文件

java - 如果使用相同的值构造,则使用现有实例

c++ - 多构造函数继承

c# - 在不可变类型的构造函数中生成 HashCode

c++ - 在 C++ 中设置变量值之前检查变量是否更好?

c++ - "stdio"和 "stdlib"在 C 中代表什么?