0

I'm trying to implement a function which return a class object using its constructor. that class object as only constructor with parameters and there isnt any default constructor (cuz i dont need it). when I try to return that class object - that error comes up..

Base.cpp

Derived Base::operator+ (){ return Derived(*this); // no matching function for call to ‘Derived::Derived(Derived)’ }

Base.h

virtual Derived operator+ ();

Derived.h //Constructors:

Derived(Base &b);

Derived(Derived &d);

1. what can I do in order to fix that error?

2. whats the diffrence between declaration (const Derived &) and (Derived &)?

Thanks

2
  • What is the purpose of operator+() here? Commented Jan 15, 2015 at 8:50
  • How does Base can see your Derived class? I assume Derived derives from Base. Commented Jan 15, 2015 at 8:53

1 Answer 1

1

Derived& will not bind to a temporary object. Your operator+ returns a temporary.

Solution: add const because const& will bind to temporaries.

Sign up to request clarification or add additional context in comments.

4 Comments

add const to both constructors?
if I add const to the constructor, I need to change the member of the constructor to const. the problem is that if so, I cannot change that object with assignment operator which I need to implement
@michal_h: No, you add const to the source object of the copy contructor. That does not affect the members, because in a copy constructor and in the assignment operator you only write to the members of the destination object.
when i write: Derived::Derived(const Derived & other){ m_d=other; } its says: uninitialized reference member 'Derived'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.