Visual Studio 2012 C++을 사용하고 있으며 고유 포인터가 서로 다른 두 벡터를 설정하려고합니다.두 개의 벡터 <unique_ptr <...>>을 서로 어떻게 설정합니까?
using namespace std;
vector<unique_ptr<Unit>> unitVector;
vector<unique_ptr<Unit>> nonDeadUnits;
.... (stuff added to unitVector) ....
for (auto unit = unitVector.begin(); unit != unitVector.end(); ++unit) {
if ((*unit)->health > 0) {
nonDeadUnits.push_back(*unit);
}
}
unitVector.clear();
unitVector = nonDeadUnits; // error here (see below error code)
내가 덜이 0보다 건강이있는 모든 유닛을 제거 할, 그러나 나는 시도하고 직접 벡터에서 제거하는 경우, 나는이 프로그램을 죽이는 안 메모리에 액세스 할 수. 그래서 내가 이런 방식으로 선택했다. 유일한 문제는 unique_ptr이 원하는 복사 유형을 허용하지 않는다는 것입니다. 여기에 오류가 : 나는 벡터 나중에 for 루프 내에서 하위 클래스 메소드를 호출하기 때문에 unique_ptr의의를하고 싶은
error C2248: 'std::unique_ptr<_Ty>::operator =' : cannot access private member declared in class 'std::unique_ptr<_Ty>' c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility 2089
, 그리고 최우선으로하는 데 도움이됩니다. 그렇다면 벡터를 서로 같게 설정하거나 더 좋은 방법이 있습니까?
'std :: remove_if'가 당신 친구일지도 모른다고 생각합니다. 예제를 작성하여 답변으로 게시하려고 노력할 것입니다. – Quuxplusone
'unitVector = std :: move (nonDeadUnits);'는 문제를 해결할 것이지만 Quuxplusone이 말한 것처럼 더 나은 해결책이 있습니다. –
"unique"ptr ..... –