Search code examples
c++classpolymorphismvirtual

Error , "include/Trading_GenericTemplate.h:37: error: expected constructor, destructor, or type conversion before â=â token"


I have a strange problem ,I am implementing Run Time polymorphism in my project which is a Trading Project(Server), here I am getting an error of instance initialization, I tried my best but couldn't get any solution. This is the code which shows compilation error which is "include/Trading_GenericTemplate.h:37: error: expected constructor, destructor, or type conversion before â=â token"

#ifndef STRAT_TRADING_TEMPLATE_H
#define STRAT_TRADING_TEMPLATE_H
class STRAT_TEMPLATE
{
    public :
     STRAT_TEMPLATE()
     {}
    virtual int EntrySignal(int instMapI, float *param)
    {}
    virtual int EntryPricer(int instMapI, float *param)
    {}
    virtual bool ExitSignal(int instMapI, float *param)
    {}
    virtual int ExitPricer(int instMapI, float *param)
    {}
    virtual void Init(int instMapI)
    {}
    
};
class B :public STRAT_TEMPLATE
{
    public :
    B()
    {}
    int EntrySignal(int instMapI, float *param)
    {}
    int EntryPricer(int instMapI, float *param)
    {}
    bool ExitSignal(int instMapI, float *param)
    {}
    int ExitPricer(int instMapI, float *param)
    {}
    void Init(int instMapI)
    {}
};
STRAT_TEMPLATE* startTrading[20];
startTrading[0] = new B();
#endif

Let me briefly describe what exactly I want to do, I need 20 instance of base class then each instance need to initialized by its different child class object, which will be defined in other header files. But after initializing its first child class object I got this error. I had implemented this link https://www.geeksforgeeks.org/virtual-functions-and-runtime-polymorphism-in-c-set-1-introduction/ code that was perfectly executed in a simple cpp program on same machine. So please help what is the problem and the solution. Thanks in advance.


Solution

  • The assignment statement startTrading[0] = new B(); is possible only in a function body, not allowed in the global scope.

    You may do

    STRAT_TEMPLATE* startTrading[20] = { new B() };