2010-04-21 7 views
5
#include <vector> 
#include <memory> 

using namespace std; 

class A { 
public: 
    A(): i(new int) {} 
    A(A const& a) = delete; 
    A(A &&a): i(move(a.i)) {} 

    unique_ptr<int> i; 
}; 

class AGroup { 
public: 
    void     AddA(A &&a) { a_.emplace_back(move(a)); } 

    vector<A> a_; 
}; 

int main() { 
    AGroup ag; 
    ag.AddA(A()); 
    return 0; 
} 

संकलन नहीं है ... (कहते हैं unique_ptr की नकल है कि निर्माता हटा दी जाती है)संकलित करने के लिए unique_ptr को शामिल करने वाला यह कोड मैं कैसे प्राप्त कर सकता हूं?

मैं आगे के साथ इस कदम की जगह की कोशिश की। यकीन नहीं है कि मैंने सही किया है, लेकिन यह मेरे लिए काम नहीं करता है।


[~/nn/src] g++ a.cc -o a -std=c++0x 
/opt/local/include/gcc44/c++/bits/unique_ptr.h: In member function 'A& A::operator=(const A&)': 
a.cc:6: instantiated from 'void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, _Args&& ...) [with _Args = A, _Tp = A, _Alloc = std::allocator<A>]' 
/opt/local/include/gcc44/c++/bits/vector.tcc:100: instantiated from 'void std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = A, _Tp = A, _Alloc = std::allocator<A>]' 
a.cc:17: instantiated from here 
/opt/local/include/gcc44/c++/bits/unique_ptr.h:219: error: deleted function 'std::unique_ptr<_Tp, _Tp_Deleter>& std::unique_ptr<_Tp, _Tp_Deleter>::operator=(const std::unique_ptr<_Tp, _Tp_Deleter>&) [with _Tp = int, _Tp_Deleter = std::default_delete<int>]' 
a.cc:6: error: used here 
In file included from /opt/local/include/gcc44/c++/vector:69, 
       from a.cc:1: 
/opt/local/include/gcc44/c++/bits/vector.tcc: In member function 'void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, _Args&& ...) [with _Args = A, _Tp = A, _Alloc = std::allocator<A>]': 
/opt/local/include/gcc44/c++/bits/vector.tcc:100: instantiated from 'void std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = A, _Tp = A, _Alloc = std::allocator<A>]' 
a.cc:17: instantiated from here 
/opt/local/include/gcc44/c++/bits/vector.tcc:314: note: synthesized method 'A& A::operator=(const A&)' first required here 
+0

कृपया त्रुटि शामिल करें। –

+0

किया गया। (जीसीसी 4.4.0) –

उत्तर

3

शायद अपने मानक पुस्तकालय (अभी तक) unique_ptr<T>::unique_ptr(unique_ptr &&) को परिभाषित करता है। मैंने अपने हेडर को 4.5 में चेक किया और यह वहां है, इसलिए शायद अपग्रेड करने का प्रयास करें।

जब यह चालक कन्स्ट्रक्टर को खोजने में विफल रहता है, तो यह प्रतिलिपि बनाने वाले को ढूंढता है और इसे हटा देता है।

मुझे संकलन करते समय मुझे अन्य त्रुटियां मिलती हैं, हालांकि।

संपादित करें: इसे काम करने के लिए मिला। मुझे समझ में नहीं आता कि आपको move क्यों एक ऑब्जेक्ट है जो पहले से ही एक रावल्यू संदर्भ है, लेकिन आप करते हैं। एकमात्र समस्या एक लापता आकलन ऑपरेटर था।

#include <vector> 
#include <memory> 

using namespace std; 

class A { 
public: 
    A(): i(new int) {} 
    A(A const& a) = delete; 
    A &operator=(A const &) = delete; 
    A(A &&a): i(move(a.i)) {} 
    A &operator=(A &&a) { i = move(a.i); } 

    unique_ptr<int> i; 
}; 

class AGroup { 
public: 
    void     AddA(A &&a) { a_.emplace_back(move(a)); } 

    vector<A> a_; 
}; 

int main() { 
    AGroup ag; 
    ag.AddA(A()); 
    return 0; 
} 
+0

वाह! मैं आपके समाधान के लिए बहुत आभारी हूं। यह मुझे कोई अंत करने के लिए निराशाजनक था। –

+0

आपके समाधान का परीक्षण किया और यह काम करता है। मैं वास्तव में उस ऑपरेटर की इच्छा करता हूं = मिलान करने वाली प्रतिलिपि का उपयोग करने के लिए डिफ़ॉल्ट। शायद 'ऑटो अवधारणाओं' के साथ, वे करेंगे? –

+1

आप शायद मेरे बारे में और अधिक जानते हैं; मैंने इसे एक त्रुटि संदेश जॉकी होने के द्वारा हल किया। – Potatoswatter

संबंधित मुद्दे

 संबंधित मुद्दे