0

Why I keep getting this error ?

Hello, I keep receiving :" no matching function for call to 'MyClass1::MyClass1()' I looked over the code and cant find any flaw so whats the problem? Here is it: #include <iostream> using namespace std; class MyClass1 { public: MyClass1(int regA,int consb) : a(regA), b(consb) { } void ceva() { } private: int a; const int b; }; int main() { MyClass1 obj; MyClass1*PtrObj = &obj; PtrObj->MyClass1(1,2); }

2nd Jun 2020, 1:08 PM
Ovidiu Călin
Ovidiu Călin - avatar
2 Answers
0
(1) you didn't define a constructor with 0 arguments, so you cannot declare MyClass1 obj; Solutions: (a) add a void constructor MyClass1() {} (b) declare MyClass1 obj(1,2) (2) PtrObj->MyClass1(1,2) is a nonsense, the object has already been constructed (3) const members must be initialized as soon as they are defined: const int b=0; (4) you must use your object to avoid a blocking warning. For example, add: cout << PtrObj;
2nd Jun 2020, 1:41 PM
Bilbo Baggins
Bilbo Baggins - avatar
0
Thanks did (b), erased (2) and worked like a charm
2nd Jun 2020, 1:48 PM
Ovidiu Călin
Ovidiu Călin - avatar