boost::bind использование в классе
От: st0nx  
Дата: 14.02.11 07:42
Оценка:
struct stringdouble
{
    std::string a;
    std::string b;
};
class HttpSendRecv_boost
{

public:
//...
private:
    std::string rStr;
    vector <stringdouble> cookie; 
    void GET_COOKIE();
    bool findvector(stringdouble ch,stringdouble s);
};


bool HttpSendRecv_boost::findvector(stringdouble ch,stringdouble s)
{
if(ch.a == s.a) 
     return true;
  return false;
};

void HttpSendRecv_boost::GET_COOKIE()
{
    stringdouble buf;
    boost::regex xRegEx("Set-Cookie: (.+?)=(.+?);");
    boost::smatch xResults;
    std::string::const_iterator xItStart = rStr.begin();
    std::string::const_iterator xItEnd = rStr.end();
    while( boost::regex_search(xItStart, xItEnd, xResults, xRegEx) )
    {
        buf.a = boost::lexical_cast<std::string>(xResults[1]);
        buf.b = boost::lexical_cast<std::string>(xResults[2]);
        if(std::find_if(cookie.begin(),cookie.end(),(boost::bind(&HttpSendRecv_boost::findvector,_1,_2,_3,buf))) == cookie.end())
        {
            cookie.push_back(buf);
        }
      xItStart = xResults[1].second;
    }
};


Я не очень понимаю как использовать внутри класса boost::bind (при написании пользовался статьей: http://www.rsdn.ru/article/cpp/boost.bind.xml
Автор(ы): Bjorn Karlsson
Дата: 24.02.2006
Рассматриваются все типовые случаи применения bind — связывание свободных функций, функций-членов класов, переменных-членов, виртуальных функций, а также функциональная композиция. На простом примере поясняется идея, лежащая в основе реализации bind.
).

Ошибки компиляции:

1>E:\Program Files\Microsoft Visual Studio 9.0\VC\include\boost/bind/bind.hpp(69) : error C2825: 'F': must be a class or namespace when followed by '::'
1> E:\Program Files\Microsoft Visual Studio 9.0\VC\include\boost/bind/bind_template.hpp(15) : see reference to class template instantiation 'boost::_bi::result_traits<R,F>' being compiled
1> with
1> [
1> R=boost::_bi::unspecified,
1> F=bool (__thiscall HttpSendRecv_boost::* )(stringdouble,stringdouble)
1> ]
1> .\Class_HTTP.cpp(152) : see reference to class template instantiation 'boost::_bi::bind_t<R,F,L>' being compiled
1> with
1> [
1> R=boost::_bi::unspecified,
1> F=bool (__thiscall HttpSendRecv_boost::* )(stringdouble,stringdouble),
1> L=boost::_bi::list4<boost::arg<1>,boost::arg<2>,boost::arg<3>,boost::_bi::value<stringdouble>>
1> ]
1>E:\Program Files\Microsoft Visual Studio 9.0\VC\include\boost/bind/bind.hpp(69) : error C2039: 'result_type' : is not a member of '`global namespace''
1>E:\Program Files\Microsoft Visual Studio 9.0\VC\include\boost/bind/bind.hpp(69) : error C2146: syntax error : missing ';' before identifier 'type'
1>E:\Program Files\Microsoft Visual Studio 9.0\VC\include\boost/bind/bind.hpp(69) : error C2208: 'boost::_bi::type' : no members defined using this type
1>E:\Program Files\Microsoft Visual Studio 9.0\VC\include\boost/bind/bind.hpp(69) : fatal error C1903: unable to recover from previous error(s); stopping compilation

Re: boost::bind использование в классе
От: Аноним  
Дата: 14.02.11 08:02
Оценка:
Здравствуйте, st0nx


S> if(std::find_if(cookie.begin(),cookie.end(),(boost::bind(&HttpSendRecv_boost::findvector,_1,_2,_3,buf))) == cookie.end())

S> {
S> cookie.push_back(buf);
S> }

Попробуйте так:

if(std::find_if(cookie.begin(),cookie.end(),(boost::bind(&HttpSendRecv_boost::findvector,this,buf,_1))) == cookie.end())
{
cookie.push_back(buf);
}
Re[2]: boost::bind использование в классе
От: st0nx  
Дата: 14.02.11 08:19
Оценка:
Здравствуйте, Аноним, Вы писали:

А>Здравствуйте, st0nx



S>> if(std::find_if(cookie.begin(),cookie.end(),(boost::bind(&HttpSendRecv_boost::findvector,_1,_2,_3,buf))) == cookie.end())

S>> {
S>> cookie.push_back(buf);
S>> }

А>Попробуйте так:


А>if(std::find_if(cookie.begin(),cookie.end(),(boost::bind(&HttpSendRecv_boost::findvector,this,buf,_1))) == cookie.end())

А>{
А> cookie.push_back(buf);
А>}

Спасибо все получилось! А не могли бы вы рассказать почему именно так?
Re[3]: boost::bind использование в классе
От: maxman20  
Дата: 14.02.11 08:37
Оценка:
Здравствуйте, st0nx, Вы писали:

S>Здравствуйте, Аноним, Вы писали:


А>>Здравствуйте, st0nx



S>>> if(std::find_if(cookie.begin(),cookie.end(),(boost::bind(&HttpSendRecv_boost::findvector,_1,_2,_3,buf))) == cookie.end())

S>>> {
S>>> cookie.push_back(buf);
S>>> }

А>>Попробуйте так:


А>>if(std::find_if(cookie.begin(),cookie.end(),(boost::bind(&HttpSendRecv_boost::findvector,this,buf,_1))) == cookie.end())

А>>{
А>> cookie.push_back(buf);
А>>}

S>Спасибо все получилось! А не могли бы вы рассказать почему именно так?


В алгоритм std::find_if нужно передать функтор который должен принимать один параметр: stringdouble,

а функция findvector, является членом класса, следовательно неявно принимает еще и указатель на объект this.

вот и получается: bind(&HttpSendRecv_boost::findvector, this, buf, _1)(stringdouble)

&HttpSendRecv_boost::findvector: указатель на функцию
this — указатель на объект класса, в котором эта функция
buf — первый аргумент
_1 — то что очталось
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.