2011-01-13 18 views
7

यदि मैं इस कोड स्निपेट में boost :: shared_ptr से std :: shared_ptr में बदलता हूं, तो मुझे लिंकर त्रुटि मिल जाएगी।boost :: shared_ptr के बीच संघर्ष को हल करने और std :: shared_ptr का उपयोग कैसे करें?

#include <iostream> 
#include <sstream> 
#include <iterator> 
#include <cctype> 
#include <cmath> 

#include <string> 
#include <vector> 
#include <stack> 
#include <queue> 
#include <set> 
#include <map> 

#include <functional> 
#include <utility> 
#include <numeric> 

#include <boost/assign.hpp> 
#include <boost/assign/std/vector.hpp> 

#include <boost/algorithm/string.hpp> 

#include <boost/test/included/unit_test.hpp> 
#include <boost/test/included/unit_test_framework.hpp> 
#include <boost/bind.hpp> 

//using namespace std; 
//using namespace boost; 

using std::string; 
using std::ostringstream; 
using namespace boost::assign; 
using namespace boost::unit_test; 

template<typename T> string to_string(T data) { ostringstream ost; ost << data; return ost.str(); } 

class TwoStringMasks { 
public: 
    string shortestCommon(string s1, string s2) { 
     //if(s1.find("*") != 0 || 
     return ""; 
    } 
}; 

class two_string_masks_test { 
public: 
    void test_case_one() { 
     string str = "TOPCODER*"; 
     BOOST_CHECK_EQUAL(str.find("*"), str.length() - 2); 
    } 
}; 

test_suite* init_unit_test_suite(int argc, char* argv[]) { 
    boost::shared_ptr<two_string_masks_test> tester(new two_string_masks_test); 
    framework::master_test_suite().add( 
     BOOST_TEST_CASE(boost::bind(&two_string_masks_test::test_case_one, tester))); 
    return 0; 
} 

त्रुटि:

Error 12 error C2784: 'T *boost::get_pointer(T *)' : could not deduce template argument for 'T *' from 'std::tr1::shared_ptr<_Ty>' c:\program files\boost\boost_1_44\boost\bind\mem_fn_template.hpp 40 1 NET Report 
Error 10 error C2784: 'T *boost::get_pointer(const std::auto_ptr<_Ty> &)' : could not deduce template argument for 'const std::auto_ptr<_Ty> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files\boost\boost_1_44\boost\bind\mem_fn_template.hpp 40 1 NET Report 
Error 11 error C2784: 'T *boost::get_pointer(const std::auto_ptr<_Ty> &)' : could not deduce template argument for 'const std::auto_ptr<_Ty> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files\boost\boost_1_44\boost\bind\mem_fn_template.hpp 40 1 NET Report 
Error 8 error C2784: 'T *boost::get_pointer(const boost::shared_ptr<X> &)' : could not deduce template argument for 'const boost::shared_ptr<X> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files\boost\boost_1_44\boost\bind\mem_fn_template.hpp 40 1 NET Report 
Error 9 error C2784: 'T *boost::get_pointer(const boost::shared_ptr<X> &)' : could not deduce template argument for 'const boost::shared_ptr<X> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files\boost\boost_1_44\boost\bind\mem_fn_template.hpp 40 1 NET Report 
Error 6 error C2784: 'T *boost::get_pointer(const boost::scoped_ptr<T> &)' : could not deduce template argument for 'const boost::scoped_ptr<T> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files\boost\boost_1_44\boost\bind\mem_fn_template.hpp 40 1 NET Report 
Error 7 error C2784: 'T *boost::get_pointer(const boost::scoped_ptr<T> &)' : could not deduce template argument for 'const boost::scoped_ptr<T> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files\boost\boost_1_44\boost\bind\mem_fn_template.hpp 40 1 NET Report 
Error 13 error C2784: 'T *boost::get_pointer(const boost::reference_wrapper<T> &)' : could not deduce template argument for 'const boost::reference_wrapper<T> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files\boost\boost_1_44\boost\bind\mem_fn_template.hpp 40 1 NET Report 
Error 14 error C2784: 'T *boost::get_pointer(const boost::reference_wrapper<T> &)' : could not deduce template argument for 'const boost::reference_wrapper<T> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files\boost\boost_1_44\boost\bind\mem_fn_template.hpp 40 1 NET Report 
Error 2 error C2784: 'optional<T>::pointer_type boost::get_pointer(boost::optional<T> &)' : could not deduce template argument for 'boost::optional<T> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files\boost\boost_1_44\boost\bind\mem_fn_template.hpp 40 1 NET Report 
Error 3 error C2784: 'optional<T>::pointer_type boost::get_pointer(boost::optional<T> &)' : could not deduce template argument for 'boost::optional<T> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files\boost\boost_1_44\boost\bind\mem_fn_template.hpp 40 1 NET Report 
Error 4 error C2784: 'optional<T>::pointer_const_type boost::get_pointer(const boost::optional<T> &)' : could not deduce template argument for 'const boost::optional<T> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files\boost\boost_1_44\boost\bind\mem_fn_template.hpp 40 1 NET Report 
Error 5 error C2784: 'optional<T>::pointer_const_type boost::get_pointer(const boost::optional<T> &)' : could not deduce template argument for 'const boost::optional<T> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files\boost\boost_1_44\boost\bind\mem_fn_template.hpp 40 1 NET Report 

क्या मैं गलत क्या किया? मैंने अनुमान लगाया कि स्टडी नेमस्पेस और नामस्थान को बढ़ावा देने के बीच एक संघर्ष था, लेकिन मुझे नहीं पता कि इसे कैसे ठीक किया जाए? कोई उपाय?

धन्यवाद,
चैन

+2

वे लिंकर त्रुटियां नहीं हैं, बल्कि संकलक त्रुटियों हैं। –

उत्तर

13

ऐसा लगता है कि आपको बूस्ट :: बाइंड के साथ std::shared_ptr का उपयोग करने के लिए get_pointer का अधिभार जोड़ने की आवश्यकता है।

namespace boost { 
    template<class T> const T* get_pointer(const std::shared_ptr<T>& ptr) 
    { 
    return ptr.get(); 
    } 

    template<class T> T* get_pointer(std::shared_ptr<T>& ptr) 
    { 
    return ptr.get(); 
    } 
} 

मुझे लगता है कि यह पहले से ही काम कर रहा है यदि आप std::bind का उपयोग करते हैं। या bind में tester.get() का उपयोग करें।

+0

वाह, आपके सुरुचिपूर्ण समाधान के लिए धन्यवाद। क्या std :: बूस्ट :: बाइंड के समान है? – Chan

+0

नहीं, यह वही नहीं है, हालांकि इंटरफ़ेस और कार्यान्वयन समान हो सकता है। हालांकि, 'std :: bind' अवधारणा को बढ़ावा संस्करण से लिया गया है। यह मानकीकरण प्रक्रिया में थोड़ा बदल गया हो सकता है। – ltjax

6

बूस्ट और TR1 साझा संकेत अलग कार्यान्वयन हैं और संगत नहीं हैं - एक या अन्य लेने और यह विशेष रूप से उपयोग करें।

+0

धन्यवाद, क्या आप मुझे दिखा सकते हैं कि कैसे? मैं इस मामले में std :: shared_ptr का उपयोग करना चाहता हूं लेकिन मैं नहीं कर सका। – Chan

+0

सबसे पहले आपको यह स्पष्ट करना चाहिए कि आप std :: tr1 :: shared_ptr का उपयोग कर रहे हैं - मेरे लिए कुछ भी नहीं निकलता है, लेकिन चूंकि मुझे यकीन नहीं है कि उस त्रुटि संदेश आउटपुट में कौन सा कॉलम लाइन नंबर होना चाहिए, यह कठिन है – bdonlan

+0

त्रुटियों से मेल खाने के लिए उन्होंने कहा कि यदि वह बूस्ट से tr1 में परिवर्तित हो जाता है तो संभवतः बाध्य के अंदर एक। लेकिन जैसा कि मैंने टिप्पणी की, उसे तब tr1 :: बाइंड का उपयोग करना चाहिए। मेरा खुद का अनुभव यह है कि बूस्ट टी 1 की तुलना में काफी बेहतर लिखा गया है, इसलिए इसके साथ चिपके रहें। – CashCow

3

बढ़ावा :: बाइंड नहीं जानता कि tr1 के साथ क्या करना है :: shared_ptr यह जानता है कि boost :: shared_ptr को कैसे संभालना है।

आप tr1 :: बाइंड का प्रयास कर सकते हैं जो शायद tr1 :: shared_ptr के साथ काम करता है।

तब एकमात्र मुद्दा यह है कि यह BOOST_TEST_CASE में काम करेगा या नहीं। अगर यह सिर्फ "रननेबल" की अपेक्षा करता है तो एक tr1 :: फ़ंक्शन केवल "रननेबल" के रूप में एक बूस्ट के रूप में होता है।

+0

इसे ठीक कर दिया! बहुत बहुत धन्यवाद। क्या tr1 :: बाइंड और बूस्ट :: एक ही बांधते हैं? (बस अलग नामस्थान)? – Chan

+0

नहीं, वे समान नहीं हैं लेकिन वे दोनों "रननेबल" कहलाते हैं, जो किसी ऐसे कार्य के लिए शब्द है जो किसी फ़ंक्शन की तरह कार्य कर सकता है, और इस अवसर पर BOOST_TEST_CASE को कार्यान्वयन या स्रोत के बारे में परवाह नहीं है जो रननेबल (यानी इसे बढ़ावा से नहीं आना है)। – CashCow

1

क्या आपने देखा है कि आप उस मामले के लिए < मेमोरी> हेडर (न ही < बूस्ट/shared_ptr.hpp> स्पष्ट रूप से शामिल नहीं हैं)?

यदि आपने कुछ और जानकारी प्रदान की है, तो यह मदद करना आसान होगा।

  1. आप किस कंपाइलर का उपयोग कर रहे हैं?
  2. क्या आप बूस्ट से अपने कंपाइलर से tr1 का उपयोग करने की उम्मीद कर रहे हैं?
  3. क्या आप shared_ptr को std :: tr1 :: shared_ptr या बस std :: shared_ptr के रूप में एक्सेस करना चाहते हैं?
+0

आपकी चिंता के लिए धन्यवाद। कंपाइलर: वीसी ++ 2010, मैं std :: shared_ptr चाहता हूं क्योंकि यह C++ 0x std है, std :: tr1 :: shared_ptr नहीं है। – Chan

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