我创建了一个模板类并对其进行了初始化。现在我想从这个类中调用一个方法,但我得到一个编译器错误。
(使用该函数TemporaryFunctionForSimulator
,我成功地将模板类拆分为 *.h 和 *.cpp。所以这不是问题!)
错误:在“模拟器”中请求成员“addEvent”,它是非类类型“模拟器*()”</p>
模拟器.h
#ifndef SIMULATOR_H
#define SIMULATOR_H
#include<queue>
#include<Event.h>
template<class T>
class Simulator
{
public:
void addEvent(T t);
Simulator();
virtual ~Simulator();
protected:
private:
};
#endif // SIMULATOR_H
模拟器.cpp
#include "Simulator.h"
#include <functional>
#include <queue>
#include <vector>
#include <iostream>
using namespace std;
template<class T>
Simulator<T>::Simulator()
{
}
template<class T>
Simulator<T>::~Simulator()
{
//dtor
}
template<class T>
void Simulator<T>::addEvent(T t)
{
//do something
}
// No need to call this TemporaryFunction() function,
// it's just to avoid link error.
void TemporaryFunctionForSimulator()
{
Simulator<int> TempObj();
}
主文件
Simulator<int> *simulator();
int t = 5;
simulator->addEvent(t); //error: request for member ‘addEvent’ in ‘simulator’, which is of non-class type ‘Simulator<int>*()’