Frage

I found a lot of questions regarding these errors but nothing has helped me. Here are my makefile, main function file, and class file used in the main. I know it is a linking problem but cannot find my error. Any help is appreciated!

Edit:Sorry...here is the error message. I am pretty stressed out and forgot to include it.

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;
    }
  }
};
War es hilfreich?

Lösung

You need to separate the definition (in Quash.h) from the implementation (in 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

And, in main.cpp, #include "Quash.h"

Andere Tipps

It looks like there's quite a bit of confusion due to some of the file contents being mislabeled in the first few edits of the question. Based on what's been posted and the error messages by thinker, it looks like what is happening is that you have two separate declarations of class Queue (one in Queue.h and another separate one in Queue.cpp that has nothing but inline functions that never get used as far as the compiler sees).

You should do one of the following:

  • move the class Quash declaration that's in Quash.cpp into the Quash.h header so the functions will be inline and available to all clients of class Quash
  • change Quash.cpp so that it has a #include "Quash.h to get the declaration of class Quash and simply implements the functions. They would end up looking like:

    #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`
    

In Quash.ccp you are declaring the class once more.

You should instead define the methods. For instance:

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

On stackoverflow you should post minimal code showing the problem you incurred in (it's a good exercise shrinking the code while keeping the error) and I have the impression you didn't do that so I'm wondering what are you doing in order to prevent copy construction and assignment.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top