【发布时间】:2015-10-12 18:58:49
【问题描述】:
我的模板类队列有问题,我一直在实现文件中实现功能,所以我看到this 的答案并决定在头文件中进行实现:
队列.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
我从我的 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);
...
}
我仍然有这个编译错误:
# 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
如果我已经在头文件中指定了实现函数,我不知道为什么会发生这种情况,我与析构函数有什么关系?
【问题讨论】:
-
你在哪里实现
~Queue()? -
~Queue()似乎没有定义。你定义了吗? -
那是错误,我做了 ~Queue(){};但因为我没有在 main 中做任何更改,它仍然给我同样的错误。