How to implement a class member which is a instance of the class I define: Get "wrong parameters count"

 
class A
   {
public:
        A(){};
   };

class B
   {
private:
        A a;
public:
        B(): a(A()) {};
   };


Hi community,

I'm trying to have a class member which is a instance of the class I define.

(In this code, trying to have a member "A a" in the class "B")


When I compile it, result is:

'A' - wrong parameters count

at second to the last line.


Q1. Is it possible to achieve what I'm trying here?

if (Q1 == true)

Q2. How should I implement?

else

Q3. Any workaround you suggest?


Thanks.

 
The default consteuctor of B is wrong.

Remove a(A())


 

I recommend you what I wrote here: https://www.mql5.com/en/forum/376954#comment_24432264

It's a lot faster than waiting here for an answer.

How to use function in EA which inherit from a Class. Get wrong parameter count.Please help
How to use function in EA which inherit from a Class. Get wrong parameter count.Please help
  • 2021.09.04
  • www.mql5.com
Hello, I need some help in finding my error in use inheritance from class CIndicatorMA...
 

Thanks. That's a solution.

I prefer to use initializer list so I wrote this way.

If I removed the A() and just leave a() empty, that seems fix the problem in my preferred way.

I don't need to call a constructor in the initializer list. All it needs is params of the constructor.

public:
        B(): a() {};


My question is now clearer.

How to use member initializer list correctly.

 
Carl Schreiber #:

I recommend you what I wrote here: https://www.mql5.com/en/forum/376954#comment_24432264

It's a lot faster than waiting here for an answer.

You are right. There is an answer in the link.

https://www.mql5.com/en/docs/basis/types/classes#initialization_list


Thanks!

Documentation on MQL5: Language Basics / Data Types / Structures, Classes and Interfaces
Documentation on MQL5: Language Basics / Data Types / Structures, Classes and Interfaces
  • www.mql5.com
Structures, Classes and Interfaces - Data Types - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
As far as I am aware, there is no need for explicit calling the default constructor of a member in the class default constructor, as it is implicitly called at definition of the "containing" class.

But I would have to verify this by code to be certain.

So my assumption is, when constructing (defining) class B, the member a gets defined and thus calls it's default constructor.


Reason: