Skip to main content
deleted 9 characters in body
Source Link
Ojmeny
  • 1.4k
  • 2
  • 14
  • 25

I'm learning c++ and I have problem with basics. How to init object in different class?

For example I have code:

class A { private: static int num; static string val; public: A(int n, string w) { num = n; val = w; } }; 

I want to create object A in class B, so I have try like this:

class B { private: A objA; public: B(int numA, string valA){ objA = new LiczbaHeksA(numA, valA); } }; 

Different ways(same constructor):

 public: B(A obA){ objA = obA; } 

or

 public: B(int numA, string valA){ objA = A(numA, valA); } 

Always I'm getting error: No default constructor for exist for class "A". I've read that default constructor is constructor without any arguments, but I give them, so why it is searching default?

I'm learning c++ and I have problem with basics. How to init object in different class?

For example I have code:

class A { private: static int num; static string val; public: A(int n, string w) { num = n; val = w; } }; 

I want to create object A in class B, so I have try like this:

class B { private: A objA; public: B(int numA, string valA){ objA = new LiczbaHeks(numA, valA); } }; 

Different ways(same constructor):

 public: B(A obA){ objA = obA; } 

or

 public: B(int numA, string valA){ objA = A(numA, valA); } 

Always I'm getting error: No default constructor for exist for class "A". I've read that default constructor is constructor without any arguments, but I give them, so why it is searching default?

I'm learning c++ and I have problem with basics. How to init object in different class?

For example I have code:

class A { private: static int num; static string val; public: A(int n, string w) { num = n; val = w; } }; 

I want to create object A in class B, so I have try like this:

class B { private: A objA; public: B(int numA, string valA){ objA = new A(numA, valA); } }; 

Different ways(same constructor):

 public: B(A obA){ objA = obA; } 

or

 public: B(int numA, string valA){ objA = A(numA, valA); } 

Always I'm getting error: No default constructor for exist for class "A". I've read that default constructor is constructor without any arguments, but I give them, so why it is searching default?

Source Link
Ojmeny
  • 1.4k
  • 2
  • 14
  • 25

How to initialize object in different class?

I'm learning c++ and I have problem with basics. How to init object in different class?

For example I have code:

class A { private: static int num; static string val; public: A(int n, string w) { num = n; val = w; } }; 

I want to create object A in class B, so I have try like this:

class B { private: A objA; public: B(int numA, string valA){ objA = new LiczbaHeks(numA, valA); } }; 

Different ways(same constructor):

 public: B(A obA){ objA = obA; } 

or

 public: B(int numA, string valA){ objA = A(numA, valA); } 

Always I'm getting error: No default constructor for exist for class "A". I've read that default constructor is constructor without any arguments, but I give them, so why it is searching default?