2011-01-13 138 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 

我做了什麼錯?我猜想std命名空間和boost命名空間之間存在衝突,但我不知道如何解決它?任何想法?

謝謝,

+2

這些不是鏈接器錯誤,而是編譯器錯誤。 – 2011-01-13 16:49:51

回答

13

它看起來像你需要添加get_pointer的重載使用std::shared_ptr與boost :: bind。

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()

6

升壓和TR1共享指針是分開的實施方式和不兼容 - 選擇一個或另一個並專門使用它。

+0

謝謝,你能告訴我如何?我想在這種情況下使用std :: shared_ptr,但我不能。 – Chan 2011-01-13 16:28:00

+0

首先你應該清楚你使用的地方std :: tr1 :: shared_ptr - 沒有東西會彈出來,但是因爲我不確定那個錯誤信息輸出中的哪一列應該是行號,所以很難以匹配錯誤 – bdonlan 2011-01-13 16:31:27

+0

他說,如果他從提升轉換爲tr1,所以大概是綁定內的那個。但正如我評論的,他必須使用tr1 :: bind。我自己的經驗是,提升比tr1寫得好得多,所以堅持下去。 – CashCow 2011-01-13 16:35:25

3

boost :: bind不知道如何處理tr1 :: shared_ptr它知道如何處理boost :: shared_ptr。

你可以嘗試tr1 :: bind,它可能適用於tr1 :: shared_ptr。

那麼唯一的問題是,這是否會在BOOST_TEST_CASE中工作。如果它只是期望一個「可運行」,那麼tr1 ::函數就像一個「可運行」一樣。

+0

搞定了!非常感謝。 tr1 :: bind&boost :: bind是否相同? (只是不同的命名空間)? – Chan 2011-01-13 16:38:41

1

您是否注意到您沒有明確包含<內存>標題(也沒有< boost/shared_ptr.hpp>)?

如果您提供了一些更多信息,它會更容易幫助。

  1. 你使用什麼編譯器?
  2. 您是否期望從boost編譯器中使用tr1?
  3. 你想訪問shared_ptr爲std :: tr1 :: shared_ptr或簡單地std :: shared_ptr?
相關問題