no matching function for call to explicit copy-constructor

Discussion in 'C++' started by Dario, Sep 22, 2003.

  1. Dario

    Dario Guest

    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
    Dario, Sep 22, 2003
    #1
    1. Advertising

  2. Dario

    Attila Feher Guest

    Dario wrote:
    > 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.

    --
    Attila aka WW
    Attila Feher, Sep 22, 2003
    #2
    1. Advertising

  3. Dario

    tom_usenet Guest

    On Mon, 22 Sep 2003 12:28:57 +0200, Dario <> wrote:

    >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
    tom_usenet, Sep 22, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jean Stax
    Replies:
    4
    Views:
    435
    lilburne
    Nov 23, 2003
  2. Christoph Bartoschek

    Explicit copy constructor

    Christoph Bartoschek, Apr 27, 2004, in forum: C++
    Replies:
    6
    Views:
    6,643
    Christoph Bartoschek
    Apr 28, 2004
  3. trying_to_learn
    Replies:
    8
    Views:
    4,704
    Victor Bazarov
    Nov 18, 2004
  4. denis

    explicit copy constructor

    denis, Jun 16, 2007, in forum: C++
    Replies:
    1
    Views:
    330
    Ian Collins
    Jun 16, 2007
  5. cinsk
    Replies:
    35
    Views:
    2,563
    James Kanze
    Oct 11, 2010
Loading...

Share This Page