1

In the class, in my header file, in the private section of the class I have

 Heap_PriorityQueue<Passenger> oldBoardingQueue;

I have #include "Heap_PriorityQueue.h" in both the header and the cpp just to be safe. I haven't even started using it in my cpp file, yet when I try to compile the cpp file throws up a bunch of:

undefiend reference to 'Heap_PriorityQueue<Passenger>::(insert function for Heap_PriorityQueue class isEmpty, add, etc.)

Followed by several

undefined reference to 'non-virtual thunk to Heap_PriorityQueue<Passenger>::(Heap_PriorityQueue functions again)

Unsure of how to proceed. Am I declaring incorrectly?

Edit:

Passenger is another class for creating Passenger objects holding the passenger's data (name, row, priority/key, etc).

Not sure what's meant by 'linker command' (I'm a student/newb) but the makefile for airworthy.cpp and airworthy.h files are as follows:

Airworthy.o: Airworthy.cpp Airworthy.h  Heap_PriorityQueue.h NotFoundException.h PrecondViolatedExcep.h Passenger.h
g++ -std=gnu++11 -ggdb -c Airworthy.cpp

Airworthy.h is as follows:

#ifndef AIRWORTHY_H
#define AIRWORTHY_H

#include "Heap_PriorityQueue.h"
#include "Passenger.h"

using namespace std;

class Airworthy
{
private:

    int oldBoardingSeconds;
    int randomBoardingSeconds;
    Heap_PriorityQueue<Passenger> oldBoardingQueue;
    Heap_PriorityQueue<Passenger> randomBoardingQueue;
    Passenger thisPassenger;

public:

    Airworthy();
    void inputFileData(ifstream& inputFile);
    void loadQueue(ifstream& inputFile, ofstream& outputFile);
    void runSim();
    void setOldBoardingPriority();
    void setRandomBoardingPriority();
};

#endif

Just realized that the error hones in on the first use of the Airworthy constructor which I've just had empty for now. Here's the first part of Airworthy.cpp

#include "Airworthy.h"
#include "Heap_PriorityQueue.h"
#include "Passenger.h"
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <cstdlib>

using namespace std;


Airworthy::Airworthy()
{
}

Tried putting different things inside the constructor (including the Heap_PriorityQueues) but doesn't seem to make a difference. Calling the constructor in the main cpp file makes that file also generate the same errors.

Heap_PriorityQueue.h is as follows:

#ifndef _HEAP_PRIORITY_QUEUE
#define _HEAP_PRIORITY_QUEUE

#include "ArrayMaxHeap.h"
#include "PriorityQueueInterface.h"

template<class ItemType>
class Heap_PriorityQueue : public PriorityQueueInterface<ItemType>,
private ArrayMaxHeap<ItemType>
{
public:
   Heap_PriorityQueue();
   bool isEmpty() const;
   bool add(const ItemType& newEntry);
   bool remove();

   /** @pre The priority queue is not empty. */
   ItemType peek() const throw(PrecondViolatedExcep);
}; // end Heap_PriorityQueue


#endif

I'm at my wits end, I've been trying to make this damned thing work for hours now.

  • Could you please show a (preferably small) example of code reproducing the behaviour in question? – Vlad May 06 '15 at 18:01
  • Also, because this sounds like a linking problem, please give us your linker command. – user1978011 May 06 '15 at 18:02
  • 2
    Or maybe your template class methods are defined in a .cpp (a [major C++ crime](https://isocpp.org/wiki/faq/templates#templates-defn-vs-decl))? – Vlad May 06 '15 at 18:05
  • Do you include the header file where class Passenger API resides? – Daniel Heilper May 06 '15 at 18:06
  • Since you mentioned including `Heap_PriorityQueue.h` into *both* you header and cpp file - are you using [include guards](http://stackoverflow.com/questions/1653958/why-are-ifndef-and-define-used-in-c-header-files)? I remember having a problem with cyclic including without include guards when I didn't know I need to use them, and your problems seems similar to mine. – Adalee May 06 '15 at 18:07
  • do you have a Heap_PriorityQueue.cpp? – NathanOliver May 06 '15 at 18:15
  • Heap_PriorityQueue.cpp and Heap_PriorityQueue.h were both provided by the text and are in the same folder as the rest of the files. – YouHaveGotToBeKiddingMe May 06 '15 at 18:17
  • It's been hours and I'm still having this error. I've even shifted Code::Blocks to use my custom makefile and it's still not working. – YouHaveGotToBeKiddingMe May 06 '15 at 20:29

1 Answers1

0

I think the likely problem is that you forgot to include Heap_PriorityQueue.cpp in the makefile recipe for Airworthy.o. So the compiler is finding the definitions for the functions of the queue, but not the funtions themselves (that is the meaning of "undefined reference" in the linking stage)

EDIT: check if the build process produces a Heap_PriorityQueue.o file, and if this file is included in the final commandline that creates the exe.

Alberto M
  • 1,057
  • 8
  • 24