c++ - Undefined reference to template class destructor -
this question has answer here:
i had problem template class queue had been implementing functions in implementation file, saw this answer , resolved implementation in 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 call function 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 microblaze instruction std::shared_ptr<microblazeinstruction> newmbinstruction; // maximum pattern size const unsigned int maxpatternsize = 300; // creating queue queue<std::shared_ptr<microblazeinstruction>> matchingqueue(maxpatternsize); ... }
and still have 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 `queue<std::shared_ptr<microblazeinstruction> >::~queue()' ./main.o:main.cpp:(.text.startup+0x83a): undefined reference `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 target 'faith.exe' failed make: *** [faith.exe] error 1
i don't know why happening if specified implementation functions in header file, have destructor?
~queue();
is not same as
~queue() {};
the second implements ~queue
, first declares it.
you declared ~queue
, defined nowhere. main destroys queue
, implicitly calls ~queue
. linker tries find it, finds nowhere, , gives error.
Comments
Post a Comment