2 questions for help!

B

Birt

1. When defining only a copy constructor but no other constructors, the
default constructor will not be available any more in a class?

2.
class CEngine
{
....
public
// CEngine();//Without this, error happens
CEngine(int volume);
....
};


class CFord : public CCar
{
CEngine e;

public:
CFord(const CFord& f);
CFord& operator=(const CFord& f);
....
};

When adding CFord(const CFord& f); into CFord, the error happens:

error C2512: 'CEngine' : no appropriate default constructor available.

To solve the problem, add a default constructor by myself. Is there any
better solution?

How to force to use the CEngine(int volume) constructor?
 
B

Buster

Birt said:
1. When defining only a copy constructor but no other constructors, the
default constructor will not be available any more in a class?

That's not a question.
2.
class CEngine
{
...
public
// CEngine();//Without this, error happens
CEngine(int volume);
...
};


class CFord : public CCar
{
CEngine e;

public:
CFord(const CFord& f);
CFord& operator=(const CFord& f);
...
};

When adding CFord(const CFord& f); into CFord, the error happens:

error C2512: 'CEngine' : no appropriate default constructor available.

To solve the problem, add a default constructor by myself. Is there any
better solution?

Well, there was a default constructor before (the implicitly declared
default constructor), and you were using it, so why not have a default
constructor now? Are you sure you no longer need one?
How to force to use the CEngine(int volume) constructor?

Pass an integer argument when the CEngine object is created. Data
members are initialized before the body of the constructor is entered,
so there is a special syntax for passing arguments to data member
constructors:

int some_integer = 0;

CFord::CFord (const CFord & f)
: e (some_integer)
{
// constructor body
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top