我正在使用 Qt5 creator ,所有文件都包含在项目中(“MyCounter”类是使用 IDE 向导创建的)我将我的代码减少到这个,当我编译和运行时:
undefined reference to MyCounter<int>::MyCounter()
主文件
#include <QCoreApplication>
#include"mycounter.h" //if include "mycounter.cpp" instead of "mycounter.h" works fine
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyCounter<int> x;
return a.exec();
}
我的计数器.h
#ifndef MYCOUNTER_H
#define MYCOUNTER_H
template<class T>
class MyCounter
{
public:
MyCounter();
};
#endif // MYCOUNTER_H
我的计数器.cpp
#include "mycounter.h"
#include <iostream>
template<class T>
MyCounter<T>:: MyCounter()
{
std::cout<<"somthing...";
}