0
class Base{
    Base();
    ~Base();
    void doSomrthing();
}

Class MyCLass : public Base{
    std::string name;
    MyCLass();
    ~MyCLass();
    void init();
}    

MyCLass::MyCLass()
{
    init(); //runtime error occurs with this line
}
void MyCLass::init()
{
    name = "MyClass";
    doSomrthing();
}

I am having a trouble on calling child class' contructor. What is wrong with this?

Edited by 9tontruck

3
Contributors
3
Replies
18
Views
3 Years
Discussion Span
Last Post by Moschops
Featured Replies
  • Could it be this Class Please note the uppercase C Or could it be the missing semi-colon here class Base { Base(); ~Base(); void doSomrthing(); } Or here Class MyCLass : public Base{ std::string name; MyCLass(); ~MyCLass(); void init(); } Read More

  • Here's a correction implementing some of the changes mentioned above #include <iostream> class Base { public: Base() {} ~Base() {} void doSomrthing() {} }; class MyCLass : public Base { std::string name; public: MyCLass(); ~MyCLass(); void init(); }; MyCLass::MyCLass() { init(); //runtime error occurs with this line } void MyCLass::init() … Read More

  • The base class' constructor and destructor are private, so an inherited class can't call them. Which is bad. Read More

1

Could it be this

Class

Please note the uppercase C

Or could it be the missing semi-colon here

class Base
{
  Base();
  ~Base();
  void doSomrthing();
}

Or here

Class MyCLass : public Base{
std::string name;
MyCLass();
~MyCLass();
void init();
} 

Edited by gerard4143

1

Here's a correction implementing some of the changes mentioned above

#include <iostream>

class Base
{
public:
  Base() {}
  ~Base() {}
  void doSomrthing() {}
};

class MyCLass : public Base
{
  std::string name;
public:
  MyCLass();
  ~MyCLass();
  void init();
};

MyCLass::MyCLass()
{
  init(); //runtime error occurs with this line
}

void MyCLass::init()
{
  name = "MyClass";
  doSomrthing();
}

int main()
{}
1

The base class' constructor and destructor are private, so an inherited class can't call them. Which is bad.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.