0

I´ve created a template class an initialized it. Now I wanna call a method from this class but I get a compiler error.

(With the Function TemporaryFunctionForSimulator I successfully split the template class in *.h and *.cpp. So that is NOT THE PROBLEM!)

Error: request for member ‘addEvent’ in ‘simulator’, which is of non-class type ‘Simulator*()’

Simulator.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

Simulator.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();
}

main.cpp

    Simulator<int> *simulator();
    int t = 5;
    simulator->addEvent(t); //error: request for member ‘addEvent’ in ‘simulator’, which is of non-class type ‘Simulator<int>*()’
alexander-fire
  • 1,082
  • 4
  • 27
  • 52
  • The problem is not the split between *.h and *.cpp! (See my question!) – alexander-fire Mar 09 '16 at 16:24
  • 1
    There is another problem not related to the duplicate, and which causes the error you get. `Simulator *simulator();` declares a function. This is known as the *most vexing parse*. Declare your object as `Simulator *simulator;` – François Moisan Mar 09 '16 at 16:24
  • Then now you should look at that duplicate. Template definitions must be visible to the compiler at the point of instantiation. You don't usually put them in cpp files, unless the only code that uses it is also in the same file. This is rarely the case. – François Moisan Mar 09 '16 at 16:34
  • Your definition of `simulator` is actually a function declaration. To fix this, take out the `()`s. You must also allocate memory for the pointer using `new`. Look up most vexing parse. – Greg M Mar 09 '16 at 17:32

0 Answers0