atributes from derived classes

I can't understand this error.

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
28
29
30
31
 class Comanda
	{
		const string _nume;

	public:
		Comanda(const string &nume);
		virtual ~Comanda();
        };


class ComandaCalcule : public Comanda
	{
		const string _nume;

		
	public:
		ComandaCalcule(const string &nume);
        };


Comanda::Comanda(const string &nume) :
		_nume(nume)
	{
	}


ComandaCalcule::ComandaCalcule(const string &nume) : //error C2512: no appropriate default constructor available
		_nume(nume)
		{
		}
Last edited on
When a derived class is created, an instance of it's base class is also created. In your case there is no default constructor for the Comanda class. That is, you need a constructor that looks like this:

Comanda::Comanda() {}

Hope all goes well
Last edited on
where exactly should i declare this constructor?
> you need a constructor that looks like this:
> Comanda::Comanda() {}
hardly a good idea
simply specify the constructor that you want to call
By instance
1
2
3
4
5
ComandaCalcule::ComandaCalcule(const string &nume) :
	Comanda("hello_world"),
	_nume(nume)
	{
	}


@OP: ¿do you realize that, because of the inheritance, a ComandaCalcule object would have two strings (both named _nume)?
In it's class header file.

1
2
3
4
5
6
class Comanda {
		const string _nume;

	public:
		Comanda(); // default constructor
		Comanda(const string &nume);


EDIT:

Even if you didn't have inheritance, it still handy to have a default ctor, with out one this happens :

// in main.cpp

1
2
Comanda MyComanda; // OK compiler makes it's own default ctor implicitly
Comanda MyComanda(); // error Comanda::Comanda() {} doesn't exist 
Last edited on
the project is for school and it's much larger..:(

for the moment i figure it out . thanks for help :D
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <string>

int main()
{
	class Comanda
	{
		const std::string _nume;

	public:
		Comanda(const std::string &nume) : _nume( nume ) {} 
		virtual ~Comanda() {}
	};


	class ComandaCalcule : public Comanda
	{
		const std::string _nume;

	public:
		ComandaCalcule(const std::string &nume) : Comanda( nume ), _nume( nume ) {}
	};

	ComandaCalcule c( "Hello, half-wit " );
}
> it still handy to have a default ctor
¿what would be the state of the object?
If it doesn't make sense, don't do it

> Comanda MyComanda();
that's a function declaration
@ne555

I am not feeling well today, and obviously it shows. I feel like 10 metres of pump water and am running on about 3 Watts. After posting this morning, I took some more drugs and went back to bed !! So no more giving advice from me for the next few days - going to hibernate instead.
Topic archived. No new replies allowed.