我发现了很多有关这些错误的问题,但没有任何帮助。这是我的makefile,main函数文件和main中使用的类文件。我知道这是一个链接问题,但找不到我的错误。任何帮助表示赞赏!

编辑:对不起...这是错误消息。我很紧张,忘了包括在内。

prog1.o: In function `main':
prog1.cpp:(.text+0x23): undefined reference to `Quash::Quash()'
prog1.cpp:(.text+0x89): undefined reference to `Quash::contains(int)'
prog1.cpp:(.text+0xa3): undefined reference to `Quash::contains(int)'
prog1.cpp:(.text+0xde): undefined reference to `Quash::insert(int)'
prog1.cpp:(.text+0x143): undefined reference to `Quash::contains(int)'
prog1.cpp:(.text+0x15d): undefined reference to `Quash::contains(int)'
prog1.cpp:(.text+0x1d7): undefined reference to `Quash::empty()'
prog1.cpp:(.text+0x208): undefined reference to `Quash::root()'
prog1.cpp:(.text+0x21c): undefined reference to `Quash::contains(int)'
prog1.cpp:(.text+0x231): undefined reference to `Quash::deleteMin()'
prog1.cpp:(.text+0x280): undefined reference to `Quash::deleteNum(int)'
prog1.cpp:(.text+0x30b): undefined reference to `Quash::deleteNum(int)'
prog1.cpp:(.text+0x36d): undefined reference to `Quash::contains(int)'
prog1.cpp:(.text+0x3bd): undefined reference to `Quash::print()'
collect2: error: ld returned 1 exit status


生成文件:

all: prog1



prog1: Quash.o prog1.o

        g++ *.o -o prog1



prog1.o: prog1.cpp

        g++ -c prog1.cpp



Quash.o: Quash.h Quash.cpp Hashtable.o Minheap.o Node.o

        g++ -c Quash.cpp



Hashtable.o: Hashtable.h Hashtable.cpp

        g++ -c Hashtable.cpp



Minheap.o: Minheap.h Minheap.cpp

        g++ -c Minheap.cpp



Node.o: Node.h Node.cpp

        g++ -c Node.cpp



clean:

        rm -f *.o

        rm -f prog1


prog1.cpp:

#include "Quash.h"
#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
  Quash* theQuash = new Quash();

  while(!cin.eof())
  {
    string cmd;
    cin >> cmd;

    if(cmd.compare("insert") == 0)
    {
      int i;
      cin >> i;

      if(theQuash->contains(i) != 0)
      {
        cout << "item already present, new count = " << theQuash->contains(i) << endl;
      }
      else
      {
        if(theQuash->insert(i))
        {
          cout << "item successfully inserted, count = 1" << endl;
        }
      }
    }
    else if(cmd.compare("lookup") == 0)
    {
      int i;
      cin >> i;

      if(theQuash->contains(i) != 0)
      {
        cout << "item found, count = " << theQuash->contains(i) << endl;
      }
      else
      {
        cout << "item not found" << endl;
      }
    }
    else if(cmd.compare("deleteMin") == 0)
    {
      if(theQuash->empty())
      {
        cout << "min item not present since table is empty" << endl;
      }
      else
      {
        int i = theQuash->root(), retVal = theQuash->contains(i);
        if(retVal == 1)
        {
          theQuash->deleteMin();
          cout << "min item " << i << " successfully deleted" << endl;
        }
        else if(retVal > 1)
        {
          int newCount = theQuash->deleteNum(i);
          cout << "min item = " << i << ", count decremented, new count = " << newCount << endl;
        }
      }
    }
    else if(cmd.compare("delete") == 0)
    {
      int i;
      cin >> i;

      int retVal = theQuash->deleteNum(i);
      if(retVal == 1)
      {
        cout << "item successfully deleted" << endl;
      }
      else if(retVal == 2)
      {
        cout << "item not present in the table" << endl;
      }
      else if(retVal == 0)
      {
        cout << "item count decremented, new count = " << theQuash->contains(i) << endl;
      }
    }
    else if(cmd.compare("print") == 0)
    {
      theQuash->print();
    }
  }
}


Quash.h

#ifndef QUASH_H
#define QUASH_H

#include "Hashtable.h"
#include "Minheap.h"


class Quash{


 private:
  Minheap heap;
  Hashtable hash;

 public:
  Quash();
  ~Quash();
  int root();
  bool empty();
  bool insert(int i);
  bool lookup(int i);
  bool deleteMin();
  int deleteNum(int i);
  void print();
  int contains(int i);


};
#endif


Quash.cpp:

#include "Hashtable.h"
#include "Minheap.h"
#include <iostream>

using namespace std;

class Quash
{
private:
  Minheap* heap;
  Hashtable* hash;

public:

  Quash()
  {
    heap = new Minheap();
    hash = new Hashtable();
  }

  ~Quash()
  {
    delete heap;
    delete hash;
  }

  int root()
  {
    return heap->getRoot();
  }

  bool empty()
  {
    if(heap->getNumElements() == 0)
    {
      return true;
    }
    else
    {
      return false;
    }
  }

  bool insert(int i)
  {
    Node* temp = new Node(i);
    if(heap->insert(temp) && hash->insert(*temp))
    {
      return true;
    }
    else
    {
      delete temp;
      return false;
    }
  }

  bool deleteMin()
  {
    return false;
  }

  int deleteNum(int i)
  {
    Node* temp = new Node(i);
    int retVal = hash->deleteNode(*temp);
    /*TODO
      HEAP DELETION
    */
    delete temp;
    return retVal;
  }
  void print()
  {
    heap->print();
    return;
  }
  int contains(int i) // lookup(i) in assignment
  {
    Node* temp = new Node(i);
    int retVal = hash->lookup(*temp);
    delete temp;
    if(retVal == 0)
    {
      return 0;
    }
    else
    {
      return retVal;
    }
  }
};

最佳答案

您需要将定义(在Quash.h中)与实现(在Quash.cpp中)分开

Quash.h

#ifndef QUASH_H_
#define QUASH_H_ // protect against multiple include

class Quash
{
    Quash(); // constructor, declare only the prototype
    // similarly for the rest of the methods
};
#endif


Quash.cpp

#include "Quash.h"
#include "Hashtable.h"
#include "Minheap.h"

// DO NOT redefine your class here, only implement its methods

Quash::Quash()
{
   // now implement the constructor
   heap = new Minheap();
   hash = new Hashtable();
};

Quash::~Quash()
{
    // and the destructor
    delete heap;
    delete hash;
}
// do the same for the rest of the methods


并且,在main.cpp中,#include "Quash.h"

关于c++ - C++中所有函数的未定义引用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23401798/

10-11 00:35