2011-01-13 20 views
7

Se cambio boost :: shared_ptr in std :: shared_ptr in questo snippet di codice, otterrò l'errore linker.Come risolvere il conflitto tra boost :: shared_ptr e usando 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; 
} 

Errore:

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 

Che cosa ho fatto di sbagliato? Ho indovinato che c'era un conflitto tra lo spazio dei nomi std e lo spazio dei nomi boost, ma non so come risolverlo? Qualche idea?

Grazie,
Chan

+2

Questi non sono errori di linker, ma errori di compilazione. –

risposta

13

Sembra che sia necessario aggiungere un sovraccarico di get_pointer per utilizzare std::shared_ptr con 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(); 
    } 
} 

Suppongo che questo funzioni già se si utilizza std::bind. Oppure utilizzare tester.get() nello bind.

+0

wow, grazie per la tua elegante soluzione. È std :: bind lo stesso di boost :: bind? – Chan

+0

No, non è la stessa cosa, anche se l'interfaccia e l'implementazione potrebbero essere identiche. Tuttavia, 'std :: bind' è concettualmente derivato dalla versione boost. Potrebbe essere stato leggermente modificato nel processo di standardizzazione. – ltjax

6

Boost e TR1 condivisi puntatori sono implementazioni separate e non sono compatibili - scegliere uno o l'altro e utilizzarlo esclusivamente.

+0

grazie, potresti mostrarmi come? Voglio usare std :: shared_ptr in questo caso, ma non ho potuto. – Chan

+0

Per prima cosa dovresti probabilmente chiarire dove stai usando std :: tr1 :: shared_ptr - niente mi viene fuori, ma dato che non sono sicuro quale colonna in quell'uscita di messaggio di errore dovrebbe essere il numero di linea, è difficile per abbinare gli errori – bdonlan

+0

Ha detto che se converte da boost a tr1 quindi presumibilmente quello all'interno del bind. Ma come ho commentato, deve usare anche tr1 :: bind. La mia esperienza è che boost è scritto molto meglio di tr1 quindi seguitelo. – CashCow

3

boost :: bind non sa cosa fare con tr1 :: shared_ptr sa come gestire boost :: shared_ptr.

Si potrebbe provare tr1 :: bind che probabilmente funziona con tr1 :: shared_ptr.

L'unico problema è se funzionerà in BOOST_TEST_CASE. Se si aspetta solo un "eseguibile", allora una funzione tr1 :: è altrettanto "eseguibile" come una spinta.

+0

risolto! Molte grazie. Tr1 :: bind & boost :: sono gli stessi? (solo uno spazio dei nomi diverso)? – Chan

+0

No, non sono uguali ma producono entrambi quello che viene chiamato un "eseguibile" che è il termine per qualcosa che può funzionare come una funzione, e in questa occasione BOOST_TEST_CASE non si preoccupa dell'implementazione o della sorgente che ha prodotto il runnable (cioè non deve venire da boost). – CashCow

1

Avete notato che non includete esplicitamente l'intestazione < memoria (né < boost/shared_ptr.hpp> per quella materia)?

Se hai fornito ulteriori informazioni, sarebbe più semplice da aiutare.

  1. Che compilatore stai utilizzando?
  2. Ti aspetti di usare tr1 dal tuo compilatore di boost?
  3. Si desidera accedere a shared_ptr come std :: tr1 :: shared_ptr o semplicemente std :: shared_ptr?
+0

grazie per la tua preoccupazione. Compiler: VC++ 2010, voglio std :: shared_ptr poiché è C++ 0x std, non std :: tr1 :: shared_ptr. – Chan

Problemi correlati