C++ 如何解决boost::shared_ptr和使用std::shared_ptr之间的冲突?
如果在此代码段中从boost::shared_ptr更改为std::shared_ptr,我将得到链接器错误C++ 如何解决boost::shared_ptr和使用std::shared_ptr之间的冲突?,c++,boost,c++11,C++,Boost,C++11,如果在此代码段中从boost::shared_ptr更改为std::shared_ptr,我将得到链接器错误 #include <iostream> #include <sstream> #include <iterator> #include <cctype> #include <cmath> #include <string> #include <vector> #include <stack>
#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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
//使用名称空间std;
//使用名称空间boost;
使用std::string;
使用std::ostringstream;
使用名称空间boost::assign;
使用名称空间boost::unit_test;
模板字符串到字符串(T数据){ostringstream ost;ostBoost和TR1共享指针是独立的实现,不兼容-选择一个或另一个并独占使用。Boost::bind不知道如何处理TR1::shared\u ptr它知道如何处理Boost::shared\u ptr
您可以尝试tr1::bind,它可能与tr1::shared\u ptr一起工作
唯一的问题是这在BOOST_TEST_情况下是否有效。如果它只需要一个“runnable”,那么tr1::函数与BOOST::bind一样“runnable”。看起来您需要添加一个get_指针重载,才能在BOOST::bind中使用std::shared_ptr
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();
}
}
namespace boost{
模板常量T*get_指针(常量std::shared_ptr&ptr)
{
返回ptr.get();
}
模板T*get_指针(标准::共享_ptr&ptr)
{
返回ptr.get();
}
}
如果您使用std::bind
,或者在bind
中使用tester.get()
,您是否注意到没有显式包含头(也没有
如果你能提供更多的信息,帮助会更容易
你在用什么编译器
您希望使用boost编译器中的tr1吗
您想以std::tr1::shared_ptr或std::shared_ptr的形式访问共享_ptr吗
谢谢,你能告诉我怎么做吗?在这种情况下,我想使用std::shared_ptr,但我不能。首先,你应该弄清楚你在哪里使用std::tr1::shared_ptr-没有任何东西会弹出给我看,但是因为我不确定错误消息输出中的哪一列应该是行号,所以很难匹配错误她说,如果他转换s从boost到tr1,所以可能是绑定中的一个。但正如我所评论的,他也必须使用tr1::bind。我自己的经验是boost编写得比tr1好得多,所以请坚持使用它。修复了它!非常感谢。tr1::bind&boost::bind是相同的吗?(只是名称空间不同)?不,它们不相同,但它们都产生了所谓的“runnable”是一个可以像函数一样工作的术语,在这种情况下BOOST_TEST_案例并不关心产生runnable的实现或源(即它不必来自BOOST)。这些不是链接器错误,而是编译器错误。谢谢你的关注。编译器:VC++2010,我想要std::shared_ptr,因为它是C++0x std,而不是std::tr1::shared_ptr。哇,谢谢你优雅的解决方案。std::bind和boost::bind一样吗?不,它不一样,尽管接口和实现可能相同。但是,std::bind
在概念上源于boost版本。它可能在标准化过程中做了一些更改。
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();
}
}