Foo f = Foo(); // no matching function for call to 'Foo::Foo(Foo)' … huh?

后端 未结 10 2730
小鲜肉
小鲜肉 2021-02-19 20:13
class Foo
{
public:
    explicit Foo() {}
    explicit Foo(Foo&) {}
};

Foo d = Foo();

error: no matching function for call to \

10条回答
  •  难免孤独
    2021-02-19 20:40

    Try without the explicit? I think that:

    Foo foo = Foo()
    

    creates an implicit copy, thus the explicit copy constructor doesn't get triggered.

    Edit:

    This is only half the answer. See Charles Bailey or UncleBens post for why const is necessary.

提交回复
热议问题