【问题标题】:boost::split argument mismatchboost::split 参数不匹配
【发布时间】:2011-12-22 09:38:26
【问题描述】:

我正在尝试将 AnsiString(someStr).c_str() 传递给 boost::split() 第二个参数,但它拒绝显示参数不匹配!!

这里是代码sn-p

vector<std::string> sVec;
boost::split(sVec,AnsiString(response).c_str(),boost::is_any_of(" "));//err in this line
ShowMessage(sVec[1].c_str());

然而

boost::split(sVec,"这是一个测试",boost::is_any_of(" "));

效果很好!

我将 AnsiString 转换为 c 字符串类型是否正确???

【问题讨论】:

    标签: boost c++builder-2010


    【解决方案1】:

    由于sVecvector&lt;std::string&gt; 而不是vector&lt;char *&gt;,因此传递给split() 的第二个参数必须以某种方式转换为std::string 实例。

    std::string 类中有一个隐式构造函数,它可以透明地从const char * 创建一个实例(这就是您的第二个示例成功的原因),但AnsiString::c_str() 返回一个char *,而不是const char * ,所以这个构造函数不适用。

    自己执行转换应该可以解决您的问题:

    boost::split(sVec, (const char *) AnsiString(response).c_str(),
        boost::is_any_of(" "));
    

    或者,更明确地说:

    boost::split(sVec, std::string((const char *) AnsiString(response).c_str()),
        boost::is_any_of(" "));
    

    【讨论】:

    • 不幸的是,两者都不起作用!不匹配! [BCC32 Error] Unit1.cpp(25): E2285 Could not find a match for 'boost::algorithm::split&lt;&gt;(vector&lt;string,allocator&lt;string&gt; &gt;,const char *,boost::algorithm::detail::is_any_ofF&lt;char&gt;)' Full parser context Unit1.cpp(22): parsing: void _fastcall TForm1::Button1Click(TObject *)
    • 模板解析显然没有考虑隐式构造函数。当您说neither works 时,您的意思是即使显式构造函数调用也会导致相同的错误?那会很奇怪,因为在这种情况下没有歧义。
    • 第二个代码 sn-p 收到的错误是否是 Could not find a match for 'boost::algorithm::split&lt;&gt;(vector&lt;string,allocator&lt;string&gt; &gt;,string,boost::algorithm::detail::is_any_ofF&lt;char&gt;)'?您使用的是哪个版本的 Boost?
    • 嘿弗雷德里克!它以以下方式工作!看!和你的完全一样!!!谢谢大佬帮忙!
    • 你之前的代码有区别吗!!我没看到!
    【解决方案2】:

    我是这样做的,因为boost::split(sVec, (const char *) AnsiString(response).c_str(), boost::is_any_of(" ")); 给出了错误(很遗憾)

    AnsiString response="This is a test";
        vector<std::string> sVec;
        const char * cStr=AnsiString(response).c_str();
        boost::split(sVec, cStr,boost::is_any_of(" "));
    
        for (int i = 0; i < sVec.size(); i++) {
                ShowMessage(sVec[i].c_str());
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-31
      • 1970-01-01
      相关资源
      最近更新 更多