+7

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

7/22/2019 12:55:21 AM

Manual

7 Answers

New Answer

+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

+4

Solved coded https://code.sololearn.com/cpzfvDFQJg5I/#cpp

+2

Alright, I filtered out all the unnessary code, then linked the code to the description.

+2

~ swim ~ Thank you!😄

+1

Thank you very much Agent_I !

+1

Manual For unbounded array int rows= 4; // can be user entered int cols = 5; using ary1d = unique_ptr<CrewMember[]>; auto ship_crew_members = make_unique<ary1d[]>(rows); for(int i=0; i < rows; ++i) { ship_crew_members[i] = make_unique<CrewMember[]>( cols); } You can also check 3D array with unique pointer here https://code.sololearn.com/co7vbW33h6ks/?ref=app

+1

Manual 👍🙂