Nested ctor

Like most of my questions, this one is an arbitrarily silly one.
How can I achieve what I'm trying to do? I'd like to avoid writing any constructor definitions in my header file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//CTOR.H
#ifndef _CTOR_H_
#define _CTOR_H_

class OutSide {
public :
	class Nested {
	public :
		Nested(int b);
		int a;
	}nested;
	OutSide();
};

#endif

//CTOR.CPP
#include "ctor.h"

OutSide::Nested::Nested(int b) {
	a=b;
}

OutSide::OutSide() {
	nested=Nested(10);
}


The compiler is complaining about line 24:
error C2512: 'OutSide::Nested' : no appropriate default constructor available
Last edited on
The nested class needs to have a default constructor. Exactly what the error code says :P

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//CTOR.H
#ifndef _CTOR_H_
#define _CTOR_H_

class OutSide {
public :
	class Nested {
	public :
		Nested(int b);
                Nested() {}; //Default ctor must be defined. Even if it is empty.
		int a;
	}nested; //It must be defined because you create an instance of it here using the default ctor.
	OutSide();
};

#endif

//CTOR.CPP
#include "ctor.h"

OutSide::Nested::Nested(int b) {
	a=b;
}

OutSide::OutSide() {
	nested=Nested(10);
}
Last edited on
> The nested class needs to have a default constructor. Exactly what the error code says
No. The error says `you are trying to use something that doesn't exists'

1
2
3
4
5
OutSide::OutSide(): //starting the initialization list
   nested(10) //specifying the constructor that you want
{ //members are already constructed here
	//nested=Nested(10); //calling assignment operator
}


There is no need for a default constructor
Thank you to both Thumper and ne555. Both your solutions worked. Thanks to Thumper for making me kick myself and thanks to ne555 for clarifying!
Last edited on
@ne555
Better answer than mine.
Topic archived. No new replies allowed.