no matching function for call to explicit copy-constructor

D

Dario

Trying to compile the following code-fragment with g++ 2.96:

class Entity {
private:
void * data;
public:
explicit Entity(int);
explicit Entity(Entity &);
virtual ~Entity();
void makeSomething();
};
Entity f(int n) {
Entity result(n);
result.makeSomething();
return result;
}

I obtain the error:
no matching function for call to `Entity::Entity (Entity &)'
on the the row:
return result;

It's a my fault?

The only way to get away from the error is to change the copy
constructor into:

/* explicit */ Entity(Entity &);

But I want to use explicit...

Is there any other option to solve my problem?

- Dario
 
A

Attila Feher

Dario said:
Trying to compile the following code-fragment with g++ 2.96:

class Entity {
private:
void * data;
public:
explicit Entity(int);
explicit Entity(Entity &);
explicit Entity(Entity const &);
virtual ~Entity();
void makeSomething();
};
Entity f(int n) {
Entity result(n);
result.makeSomething();
return result;
}

I obtain the error:
no matching function for call to `Entity::Entity (Entity &)'
on the the row:
return result;

It's a my fault?

Yes. You made the copy constructor explicit.
The only way to get away from the error is to change the copy
constructor into:

/* explicit */ Entity(Entity &);

But I want to use explicit...

Is there any other option to solve my problem?

Not really. Making the copy constructor explicit does not seem to be a good
idea.

Of course - in any other case - you could write Entity(result), but not in a
return statement. That would count as copy initialization of the return
value and an accessible (implicit) copy constructor has to be present.

I think you have no choice but to remove the explicit.
 
T

tom_usenet

Trying to compile the following code-fragment with g++ 2.96:

class Entity {
private:
void * data;
public:
explicit Entity(int);
explicit Entity(Entity &);

Are you sure you don't want:
Entity(Entity const&);
? If not, why not?
virtual ~Entity();
void makeSomething();
};
Entity f(int n) {
Entity result(n);
result.makeSomething();
return result;
}

I obtain the error:
no matching function for call to `Entity::Entity (Entity &)'
on the the row:
return result;

It's a my fault?

Yes, since you made the copy constructor "explicit".
The only way to get away from the error is to change the copy
constructor into:

/* explicit */ Entity(Entity &);

But I want to use explicit...

Why? Making a copy constructor explicit is done precisely to prevent
that type from being returned by value or used as a pass-by-value
parameter to a function, while still allowing local copies to be made.
What you you using explicit for?

Tom
 

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
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top