2014-09-08 3 views
1

내가 unique_ptr<Card>의 벡터를 보유 Deck 수업을 시도하지만,이 오류의 벡터 결과를 정렬 시도하고있다 : VS에 unique_ptr의 벡터를 정렬 2,013

Error 1 error C2280: 'std::unique_ptr>:: unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)' : attempting to reference a deleted function

가 스택 오버플로를 통해 찾고를, 그것을 벡터가 벡터 대신 unique_ptr을 복사하는 대신 잘못 복사하려고하는 VS 2013에있는 것처럼 보이므로 내 Deck 클래스에 내 이동 기능을 추가하려고 시도했지만 여전히 오류가 발생합니다.

Deck.h :

#include "Card.h" 
#include <vector> 
#include <memory> 

class Deck 
{ 
public: 
typedef std::unique_ptr<Card> cardPtr; 

Deck(); 

Deck(Deck && other) 
    : mDeck(std::move(other.mDeck)) 
{ 
} 

Deck& operator=(Deck other) 
{ 
    swap(*this, other); 
    return *this; 
} 

friend void swap(Deck& lhs, Deck& rhs); 

void        sortDeck(); 

private: 
static bool       compareCards(cardPtr A, cardPtr B); 

private: 
std::vector<cardPtr>    mDeck; 
}; 

Deck.cpp : 여기

문제의 코드의 최소 예 ( Card가 없음 개체 단지 더미 클래스)이다

#include "Deck.h" 
#include <algorithm> 


Deck::Deck() 
{ 
} 

void swap(Deck& lhs, Deck& rhs) 
{ 
using std::swap; 
swap(lhs.mDeck, rhs.mDeck); 
} 


bool Deck::compareCards(cardPtr A, cardPtr B) 
{ 
return true; //dummy- normally would have logic here 
} 

void Deck::sortDeck() 
{ 
std::sort(mDeck.begin(), mDeck.end(), compareCards); //bug happens here 
} 

해결 방법에 대한 의견이 있으십니까? 나는 분명히 무언가를 놓치고있을 것임에 틀림 없다. 그러나 나는 이것에 반대하여 머리를 때리고, 상당한 시간 동안 그것을 검색하고 도움을받을 수 있었다.

답변

4

귀하의 compareCards 기능들이 unique_ptr 복사 생성자가 암시 적으로 삭제됩니다 (복사 가능한 아니기 때문에이 때문에 이동 생성자의 존재로 작동하지 않습니다 값으로 unique_ptr의 소요하는 복사 가능한 unique_ptr은 매우 독특한 것이다되지 않을 것 그것?).

bool compareCards(cardPtr const& A, cardPtr const& B);

+0

Doh! 그것은 정확하게 그 것이었다. 감사! – JordanW

0

익명 함수에

변경이 가능하고 현재 상황에서 더 낫다. 이렇게하면 여러 클래스 함수가 ​​모든 것을 편리하게 만드는 것을 방지 할 수 있습니다.

void sortUniquePtrObjects() 
{ 
    std::sort(array.begin(), array.end(), 
       [&](const std::uniquePtr<ObjectName> &A, const std::uniquePtr<ObjectName> &B) 
       { 
        return A->comparableItem() == B->comparableItem(); 
       }); 
} 
관련 문제