C++ c++;模板类-方法中的类变量不可用
我对模板类一无所知。 我已经创建了一个,但是我无法访问方法中的公共类变量 程序正在崩溃。 在调试模式下观察:没有名为q的成员。-->撞车 类别:C++ c++;模板类-方法中的类变量不可用,c++,templates,C++,Templates,我对模板类一无所知。 我已经创建了一个,但是我无法访问方法中的公共类变量 程序正在崩溃。 在调试模式下观察:没有名为q的成员。-->撞车 类别: #ifndef SIMULATOR_H #define SIMULATOR_H #include<queue> #include<Event.h> #include <functional> #include <vector> #include <iostream> using namesp
#ifndef SIMULATOR_H
#define SIMULATOR_H
#include<queue>
#include<Event.h>
#include <functional>
#include <vector>
#include <iostream>
using namespace std;
template<class T>
class Simulator
{
public:
std::priority_queue<Event<T>*> q;
int i;
Simulator()
{
}
virtual ~Simulator()
{
//dtor
}
void addEvent(Event<T> *e)
{
i = 5; //Watch in debug mode: There is no member named i. --> CRASH
this->q.push(e); //Watch in debug mode: There is no member named q. --> CRASH
}
};
#endif // SIMULATOR_H
#ifndef模拟器
#定义模拟器
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
模板
类模拟器
{
公众:
std::优先级队列q;
int i;
模拟器()
{
}
虚拟仿真器()
{
//dtor
}
无效事件(事件*e)
{
i=5;//在调试模式下观察:没有名为i的成员。-->CRASH
this->q.push(e);//在调试模式下观察:没有名为q的成员。-->CRASH
}
};
#endif//SIMULATOR\u H
main.cpp
Simulator<int> *simulator;
simulator->addEvent(event);
Simulator*模拟器;
模拟器->附加事件(事件);
模拟器*模拟器=新的模拟器;
模拟器->附加事件(事件);
您创建了一个未初始化的指针,因此程序崩溃,因为指针未指向有效对象
最好避免使用指针,因为在这种情况下(没有看到代码),我看不出有任何理由使用它。Simulator*Simulator=new Simulator;
模拟器->附加事件(事件);
您创建了一个未初始化的指针,因此程序崩溃,因为指针未指向有效对象
最好避免使用指针,因为在这种情况下(没有看到代码),我看不出有任何理由使用它。事件类的代码在哪里?事件对象主要声明在哪里?事件类的代码在哪里?事件对象主要在哪里声明?
Simulator<int> *simulator = new Simulator<int>;
simulator->addEvent(event);