1

i have 2 classes, one class which inherits it's parameters from an abstract class:

class Child : public Base {
public:
    Child(string s, int i, ) : Base(s, i){};
    ... // methods
};

and another which has two overloaded constructors, one uses normal parameters and another, gets the same parameters but from the first class' already existing object:

header file:

class Other {
private:
    string s;
    int i; 
    Child o;
public:
    Other(string str, int num);
    Other(Child ob);
};

cpp file:

Other :: Other(string str, int num) : s(str), i(num) {/* this is where the error happens*/};
Other :: Other(Child ob) : o(ob) {
};

but when i try to compile i get an error at the marked place "C2512 'Other': no appropriate default constructor available"

What could be the problem? i really need to to pass that object into the constructor

Yokubasu
  • 25
  • 3
  • 8

4 Answers4

2

Here:

Other :: Other(string str, int num) : s(str), i(num)

you need to construct the child object:

Other :: Other(string str, int num) : s(str), i(num), o(str, num ) {}
1

You don't have Child::Child(). Since you don't list o in the initializer list in the error line, Child::Child() is getting called. This empty constructor is automatically added when there is no other constructor. Given that you have Child::Child(string s, int i), compiler will not auto create Child::Child().

xosp7tom
  • 2,131
  • 1
  • 17
  • 26
  • 1
    yeah this was a stupid mistake. I went to university to study software engineering because i liked it in IT classes at school, but all this OOP, pointer and similar high end stuff scares me a lot. When i see other people' code on this site it makes me feel really discouraged and i feel like i will never be a good programmer – Yokubasu Dec 12 '16 at 13:01
0

This occurs because Other has a Child member, but you haven't given Child a default constructor (a constructor that takes no arguments). Since Child doesn't have a default constructor, the compiler has no idea how to create an instance of Child, so you have to tell it.

Other :: Other(string str, int num) : s(str), i(num), o(some_value_of_type_Child) {};
IanPudney
  • 5,941
  • 1
  • 24
  • 39
  • 1
    You are also passing by value to the constructor, an instance of a class that must have its copy CTOR overloaded.Where is this Child's canonical class definition. I would like to see it. – Abstraction is everything. Dec 11 '16 at 21:31
0

I'm just guessing here but I suspect the Other constructor taking a string and an integer is supposed to use those arguments to construct the Child object.

Then you should do something like this instead:

Other:: Other(string str, int num) : o(str, num) {}

And of course remove the s and i member variables from the Other class.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • not exactly, in main() i have an initialized Child object, and then i want to make it so that i can create Other class object either by directly inputting values into the Other constructor or by passing a Child object and using it's values – Yokubasu Dec 11 '16 at 20:30
  • @Yokubasu So what is the purpose of the two-argument `Other` constructor? What are the arguments for? – Some programmer dude Dec 11 '16 at 20:33
  • To put in context in my program i'm writting a class to calculate Human Development Index (HDI for short). to do that you need to calculate other smaller indexes, which i did in another class, and to calculate those you need various statistics of a country (inherited from Base class). what i want to do is to give user a choice: if he has country's statistics, use the constructor using the object of the indexes class, but if he has the indexes themselves but no statistics, use the other constructor. This program doesn't have any real use i'm just trying to fulfill requirement for homework – Yokubasu Dec 11 '16 at 20:59