-3

I don't have a lot of experience with unique_ptrs yet, and I'm having a tough time determining why I get this error message:

error C2664: 'IFCC::IFCC(const IFCC &)' : cannot convert argument 1 from 'std::unique_ptr>' to 'IFO *' \Microsoft Visual Studio 12.0\VC\include\memory

This is the code that I think is causing this compilation error:

IFP.cpp:

std::unique_ptr<IFCC> IFPage::getIFCC()
{
    return(make_unique<IFCC>(m_IFCC));  //recent add that is probably causing issue
}

IFP.h:

public:
   std::unique_ptr<IFCC> getIFCC();
private:
   unique_ptr<IFCC>  m_IFCC = nullptr;

For some reason the error mentions IFO, which is the heirarchical parent of IFP. Also, IFCC has a parent of IFCCB, and both IFCC and IFCCB take a parameter of IFO. If I'm just making a new unique_ptr of IFCC, I don't think it would need the parameter that IFCC requires to make a new object, since I'm using an existing object to make it.

It's probably some syntax error that I missed somewhere. It doesn't seem like returning a value should cause issues.

I've been looking at unique_ptr info and it looks like I have what they have, but my application is more complicated. So do you have any input as to the error?

I've also seen stack overflow unique_ptr link and think that it is a different question because they are asking how it is implemented for the compiler and why, whereas my question is regarding how to implement the unique_ptr for a return value. In their question, one return statement is shown, but I am trying to use make_unique, whereas they created a new unique_ptr and used that variable as a return value.

Community
  • 1
  • 1
Michele
  • 3,617
  • 12
  • 47
  • 81

1 Answers1

2

You do not understand the function make_unique which takes variadic arguments, that is, it will create the templated type by forwarding the arguments you pass to it. One of the brilliant features of C++11 that means you don't need to create loads of templates for 1, 2, 3, 4 etc. arguments anytime you want to use this concept.

In your case you are trying to invoke a constructor:

IFCC::IFCC( unique_ptr< IFCC > )

which I guess does not exist. Instead the compiler is trying to use your copy-constructor which takes a const reference to IFCC, and it cannot convert the unique_ptr to that type.

If that is the constructor you are trying to use, i.e. copy-construct the object you would need:

make_unique( *m_IFCC );

You will need potentially to check that you have an object. Your constructor defaults the member to nullptr and if it still holds that, you'll probably want to also return a nullptr unique_ptr.

My guess is that you don't want to return a unique_ptr at all. It's called unique for a reason. you only have one of them. So if you are trying to pass "by reference" the member variable, your class has to "disown" it.

What you probably want to actually return is just a reference or pointer to the underlying object. Alternatively, given you seem to be copying it, just return it by value giving the caller a copy.

If the reason you want to return unique_ptr is because it is the base class to something (usually a good reason to return unique_ptr) you will of course want to implement the function not to invoke the copy constructor, which would slice your object.

Reeturning unique_ptr to do this is something normally done when you are creating a brand new object, not copying an existing one, and certainly not when attempting to give access to a member by reference.

Using a "disowned" object is undefined behaviour, the only thing that is safe to do is let its destructor get invoked.

CashCow
  • 30,981
  • 5
  • 61
  • 92
  • Your constructor is a copy constructor, it expects a reference to an IFCC, I can see it from the error message generated. It does not take a unique_ptr I showed you the solution – CashCow Jan 08 '15 at 14:58