c++中所有函数的未定义引用

Undefined Reference for all functions in c++

本文关键字:未定义 引用 函数 c++      更新时间:2023-10-16

我发现了很多关于这些错误的问题,但没有任何帮助。以下是我的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

Makefile:

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"

由于在问题的前几次编辑中,一些文件内容被错误标记,看起来有点混乱。根据发布的内容和思想者的错误消息,看起来发生的事情是class Queue有两个单独的声明(一个在Queue.h中,另一个在Queue.cpp中,除了编译器认为永远不会使用的内联函数外,什么都没有(。

您应该执行以下操作之一:

  • Quash.cpp中的class Quash声明移动到Quash.h标头中,以便函数是内联的,并且可用于class Quash的所有客户端
  • 更改Quash.cpp,使其具有#include "Quash.h以获得class Quash的声明,并简单地实现函数。他们最终会看起来像:

    #include <iostream>
    #include "Hashtable.h"
    #include "Minheap.h"
    #include "Quash.h"
    // note: there is no class Quash { ... } surrounding these functions -
    //       the class has already been declared in Quash.h, the .cpp file 
    //       only contains the function defintions for any functions that 
    //       are not defined inline in the .h file  (and definitions of 
    //       static data members)
    Quash::Quash()
    {
        heap = new Minheap();
        hash = new Hashtable();
    }
    Quash::~Quash()
    {
        delete heap;
        delete hash;
    }
    // etc for each of the functions delcared (but not inline-defined) in `Quash.h`
    

在Quash.ccp中,您将再次声明该类。

相反,您应该定义方法。例如:

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

在stackoverflow上,你应该发布最少的代码来显示你遇到的问题(这是一个很好的练习,在保持错误的同时缩小代码(,我觉得你没有这样做,所以我想知道你在做什么来防止复制构造和分配。