+ 8

How may I properly declare an object array using unique_ptr along with make_unique [ Solved ]

Hello coders, I am trying to declare and initialize a unique_ptr holding a class array This is a sample I am using to solve a memory management issue with my project. I can declare the pointer but I am not able to initialize it. I received the errors: errors recieved: call to non-constexpr function 'void* operator new ' std::unique_ptr ship_crew_members = std::make_unique< new CrewMember[3][3]>; ^ cannot resolve overloaded function 'make_unique' based on conversion to type 'std::unique_ptr' std::unique_ptr ship_crew_members = std::make_unique< new CrewMember[3][3]>; Altered and simplified sample code: https://code.sololearn.com/cT2O1JW3WH3p/#cpp

22nd Jul 2019, 12:55 AM
Manual
Manual - avatar
7 Respostas
+ 4
According to the reference in https://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique Construction for array type with known bound is not allowed. So I tried changing it to std::unique_ptr<CrewMember[][3]> ship_crew_members = std::make_unique< CrewMember[][3]>(3); And it works just fine
22nd Jul 2019, 3:01 AM
Agent_I
Agent_I - avatar
22nd Jul 2019, 3:26 AM
Manual
Manual - avatar
+ 2
Alright, I filtered out all the unnessary code, then linked the code to the description.
22nd Jul 2019, 2:11 AM
Manual
Manual - avatar
+ 2
~ swim ~ Thank you!😄
22nd Jul 2019, 7:52 AM
Manual
Manual - avatar
+ 1
Thank you very much Agent_I !
22nd Jul 2019, 3:11 AM
Manual
Manual - avatar