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?

Recommended Answers

All 3 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();
} 

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()
{}

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.