Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 未定义对'的引用;HashTable::HashTable()';_C++_Reference - Fatal编程技术网

C++ 未定义对'的引用;HashTable::HashTable()';

C++ 未定义对'的引用;HashTable::HashTable()';,c++,reference,C++,Reference,我正在为一个实验室做一个测试程序,我不断地发现错误 [jereminp@bondi hw3]$ make insert_test g++ -O3 -g -o insert_test.o -c insert_test.cc g++ -O3 -g -o insert_test insert_test.o insert_test.o: In function `main': /users/ugrad/2018/fall/jereminp/114/hw3/hw3/insert_test.cc:17:

我正在为一个实验室做一个测试程序,我不断地发现错误

[jereminp@bondi hw3]$ make insert_test
g++  -O3 -g -o insert_test.o -c insert_test.cc
g++ -O3 -g -o insert_test insert_test.o
insert_test.o: In function `main':
/users/ugrad/2018/fall/jereminp/114/hw3/hw3/insert_test.cc:17: undefined reference to `HashTable::HashTable()'
/users/ugrad/2018/fall/jereminp/114/hw3/hw3/insert_test.cc:24: undefined reference to `HashTable::insert(int)'
/users/ugrad/2018/fall/jereminp/114/hw3/hw3/insert_test.cc:32: undefined reference to `HashTable::insert(int)'
collect2: ld returned 1 exit status
make: *** [insert_test] Error 1
我相当肯定我有逻辑,但我无法克服这个错误,我觉得我错过了一些简单的东西。下面是有问题的代码部分

生成文件

CC = g++
CFLAGS =
COPTFLAGS = -O3 -g

insert_test: insert_test.o
    $(CC) $(COPTFLAGS) -o $@ $^
插入_test.cc

#include "HashTable.h"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <math.h>

using namespace std;
class HashTable;

int main(int argc, char* argv[])
{
  //checks .5 and .9 1x
  int numRuns = 0;
  int numProbes = 0;
  int a=0;
  HashTable h;
HashTable.cc

#include "HashTable.h"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <math.h>

using namespace std;

class HashTable
{
  public:


  HashTable()
  {
    load = 0;
    nSlot = 300;//nSlot is changable in order to optimize
    nElem = 0;
    arr = new int[nSlot];
    probesSearch=0;
    for(int i = 0;i<nSlot;i++)
    {
      arr[i] = NULL;
    }
  }
#包括“HashTable.h”
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
类哈希表
{
公众:
哈希表()
{
载荷=0;
nSlot=300;//nSlot可以更改以进行优化
nElem=0;
arr=新整数[nSlot];
probesSearch=0;

对于(int i=0;IION Mag文件不显示您编译<代码> HasTaby.cc,仅<代码>插入txTest.cc/COD>。您的程序违反了一个定义规则;它在两个翻译单元中定义了<代码>类哈希表< /> >。请参阅您最喜欢的C++教科书,以获得一个成员函数的类外定义的正确语法。
arr=new int[nSlot];
没有对应的
delete[]
项。在使用
new
之前,请考虑一下,因为它可能会咬到您。您必须在链接时为哈希表链接对象。
HashTable.cc
不应具有
类哈希表{…
一点也不。你已经在头文件中有了它。你不能在实现文件中重新定义类。要在实现文件中实现类成员,你需要使用构造函数:
HashTable::HashTable(){//constructor的实现在这里}
你的
c++
书应该解释这一点。
#include "HashTable.h"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <math.h>

using namespace std;

class HashTable
{
  public:


  HashTable()
  {
    load = 0;
    nSlot = 300;//nSlot is changable in order to optimize
    nElem = 0;
    arr = new int[nSlot];
    probesSearch=0;
    for(int i = 0;i<nSlot;i++)
    {
      arr[i] = NULL;
    }
  }