2

I had a problem with my template class Queue which I had been implementing the functions in a implementation file, so I saw this answer and resolved to do the implementation in the header file:

Queue.hpp

#ifndef QUEUE_HPP
#define QUEUE_HPP

#include "Instruction.hpp"
#include  "MicroblazeInstruction.hpp"

#include <memory>
#include <list>

template<typename T>
class Queue{
public:
    Queue(unsigned int max): maxSize{max} {};
    ~Queue();

    std::list<T> getQueue(){
        return queue;
    };

    void push(T obj){
        if(queue.size() < maxSize){
            queue.push_front(obj);
        }
        else{
            queue.pop_back();
            queue.push_front(obj);
        }
    };

private:
    Queue(const Queue&);
    Queue& operator=(const Queue&);
    unsigned int maxSize;
    std::list<T> queue;
};


#endif

And I call this function from my main:

#include "icm/icmCpuManager.hpp"
#include "Instruction.hpp"
#include "MicroblazeInstruction.hpp"
#include "CpuManager.hpp"
#include "File.hpp"
#include "Utils.hpp"
#include "MbInstructionDecode.hpp"
#include "Queue.hpp"
#include "PatternDetector.hpp"

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <sstream>
#include <cstdint>
#include <memory>

int main(int argc, char *argv[]){
    ...

    // Create pointer to microblaze instruction
    std::shared_ptr<MicroblazeInstruction> newMbInstruction;

    // Maximum pattern size
    const unsigned int maxPatternSize = 300;

    // Creating the Queue
    Queue<std::shared_ptr<MicroblazeInstruction>> matchingQueue(maxPatternSize);    

    ...
}

And I still have this compilation error:

# Linking Platform faith.exe
g++ ./CpuManager.o ./Instruction.o ./File.o ./Utils.o ./MicroblazeInstruction.o ./MbInstructionDecode.o ./PatternDetector.o ./main.o -m32 -LC:\Imperas/bin/Windows32 -lRuntimeLoader -o faith.exe
./main.o:main.cpp:(.text.startup+0x552): undefined reference to `Queue<std::shared_ptr<MicroblazeInstruction> >::~Queue()'
./main.o:main.cpp:(.text.startup+0x83a): undefined reference to `Queue<std::shared_ptr<MicroblazeInstruction> >::~Queue()'
c:/mingw/mingw32/bin/../lib/gcc/i686-w64-mingw32/4.7.0/../../../../i686-w64-mingw32/bin/ld.exe: ./main.o: bad reloc address 0x0 in section `.ctors'
collect2.exe: error: ld returned 1 exit status
makefile:24: recipe for target 'faith.exe' failed
make: *** [faith.exe] Error 1

I don't know why is this happening if I already specified the implementation functions in the header file, what do I have to do with the destructor?

Community
  • 1
  • 1
joaomlap
  • 195
  • 3
  • 13

2 Answers2

5
~Queue();

is not the same as

~Queue() {};

the second implements ~Queue, the first just declares it.

You declared a ~Queue, but defined it nowhere. Your main destroys a Queue, which implicitly calls ~Queue. The linker tries to find it, finds it nowhere, and gives you an error.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
  • It was that, I even did that but because I didn't do any changes with main it was compiling the same code as before. – joaomlap Jul 22 '15 at 18:23
1

In C++, there's a clear distinction between declaration and definition. In your header file, the destructor ~Queue() is declared. But the destructor ~Queue's definition is nowhere to be found. The compile actually was successful but the linker will complaint if it can not find the implementation of your destructor in this case.

Kevin Le - Khnle
  • 10,579
  • 11
  • 54
  • 80