在c++中使用boost正则表达式匹配从字符串转换为整型时出错

Error trying to convert from string to int using boost regex match in c++

本文关键字:转换 字符串 整型 出错 c++ 正则表达式 boost      更新时间:2023-10-16

我正试图将匹配的字符串转换为使用regex/boost的int。我使用这个c++将Boost Regex匹配结果转换为其他格式作为参考。然而,当我尝试时,我得到了expected primary-expression before ‘int’Symbol 'lexical_cast' could not be resolved错误。

这是我的代码:

#include <iostream>
#include <string>
#include <boost/regex.hpp>
using namespace std;
using namespace boost;

int main(){
    string a = "123";
    boost::regex e("123");
    boost::smatch match;
    if (boost::regex_search(a, match, e))
        {
            int number = boost::lexical_cast<int>(match[0]);
            cout << number << endl;
         }
    return 0;
}

为什么我得到这些错误?

你忘了这一行:

#include <boost/lexical_cast.hpp>