Pass string to object contructor

 

Hi folks,

Maybe I have dumb question, but I'm not abel to solve following issue for a while in MQL4:

I have an object with string attribute I'd like to initialize in constructor:

class C_instrument
{
   private: 
      string                  str_name;                       //!< Instrument name  
              
// ---------------------------------------------------------------------------------------------------------      
   public:

  
 /*! \brief Parametric constructor - automatic
   !*/
   void C_instrument(string str_initName)
   {        
      str_name = str_initName;                            
   } 
};

This part is compiled without errors. Trying to call the object contructor:

string                           str_EAF_symbolName;           //!< Symbol name
.
.
.
.
/*! \brief Initialization function
!*/
int OnInit()
{
   // get current period
   e_EAF_currentPeriod = (ENUM_TIMEFRAMES)(Period());
   str_EAF_symbolName = Symbol();
   
   c_EAF_instrument = new C_instrument(str_EAF_symbolName);

I'm getting following compile error:

"'C_instrument' - parameter passed as reference, variable expected"


Any idea how to fix it?
 
Matous Bartl: Any idea how to fix it?
c_EAF_instrument = new C_instrument(str_EAF_symbolName);
  1. Don't post code that will not compile; unknown variable c_EAF_instrument. Post all relevant code.
  2. You created an object with new. Then you try to assign the pointer to a object.
  3. Just create your object and assign it to your global object.
    c_EAF_instrument = C_instrument(str_EAF_symbolName);
  4. or just create your global object on load
    C_instrument c_EAF_instrument(_Symbol);