【问题标题】:Error using regex in custom library在自定义库中使用正则表达式时出错
【发布时间】:2013-12-21 19:07:10
【问题描述】:

我关注了这个link,一切正常。现在我尝试将这个函数rreplace 像这样放在我自己的库中

myLib.h的内容

#include <regex>

using namespace std;

class myLib {

    private:
        // Some private things

    public:
        // Some public things
        int rreplace (char *buf, int size, regex_t *re, char *rp);
};

和对应的myLib.cpp

#include "myLib.h"
// other includes


// other library functions


int rreplace (char *buf, int size, regex_t *re, char *rp){
    char *pos;
    int sub, so, n;
    regmatch_t pmatch [10]; /* regoff_t is int so size is int */
    if (regexec (re, buf, 10, pmatch, 0))
        return 0;
    for (pos = rp; *pos; pos++)
        if (*pos == '\\' && *(pos + 1) > '0' && *(pos + 1) <= '9'){
            so = pmatch [*(pos + 1) - 48].rm_so;
            n = pmatch [*(pos + 1) - 48].rm_eo - so;
            if (so < 0 || strlen (rp) + n - 1 > size)
                return 1;
            memmove (pos + n, pos + 2, strlen (pos) - 1);
            memmove (pos, buf + so, n);
            pos = pos + n - 2;
        }

    sub = pmatch [1].rm_so; /* no repeated replace when sub >= 0 */
    for (pos = buf; !regexec (re, pos, 1, pmatch, 0); ){
        n = pmatch [0].rm_eo - pmatch [0].rm_so;
        pos += pmatch [0].rm_so;
        if (strlen (buf) - n + strlen (rp) + 1 > size)
            return 1;
        memmove (pos + strlen (rp), pos + n, strlen (pos) - n + 1);
        memmove (pos, rp, strlen (rp));
        pos += strlen (rp);
        if (sub >= 0)
            break;
    }
    return 0;
}

现在当我尝试使用以下方法编译这个库时

g++ -c myLib.cpp

它给我带来了这些我不知道为什么的错误

In file included from /usr/include/c++/4.6/regex:35:0,
                 from myLib.h:4,
                 from myLib.cpp:1:
/usr/include/c++/4.6/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.
In file included from myLib.cpp:1:0:
myLib.h:36:38: error: ‘regex_t’ has not been declared
myLib.cpp: In member function ‘void myLib::replaceDotsWithCommas()’:
myLib.cpp:18:2: error: ‘regex_t’ was not declared in this scope
myLib.cpp:18:10: error: expected ‘;’ before ‘re’
myLib.cpp:23:12: error: ‘re’ was not declared in this scope
myLib.cpp:23:38: error: ‘REG_ICASE’ was not declared in this scope
myLib.cpp:23:47: error: ‘regcomp’ was not declared in this scope
myLib.cpp: At global scope:
myLib.cpp:114:36: error: ‘regex_t’ has not been declared
myLib.cpp: In function ‘int rreplace(char*, int, int*, char*)’:
myLib.cpp:117:2: error: ‘regmatch_t’ was not declared in this scope
myLib.cpp:117:13: error: expected ‘;’ before ‘pmatch’
myLib.cpp:118:28: error: ‘pmatch’ was not declared in this scope
myLib.cpp:118:37: error: ‘regexec’ was not declared in this scope
myLib.cpp:122:9: error: ‘pmatch’ was not declared in this scope
myLib.cpp:131:8: error: ‘pmatch’ was not declared in this scope
myLib.cpp:132:49: error: ‘regexec’ was not declared in this scope

我正在尝试用谷歌搜索它,但没有运气。如果问题太基本,请见谅。

【问题讨论】:

  • 试试int myLib::rreplace(char *buf, int size, regex_t *re, char *rp){}
  • 您可能需要指定正则表达式命名空间。我使用 boost 并且通常在我的标题中添加这样的内容:#include &lt;boost/regex.hpp&gt;using namespace std;using namespace stdext;using namespace boost;

标签: c++ regex linux compiler-errors


【解决方案1】:

错误信息指出您需要使用选项-std=c++0x进行编译。

g++ -std=c++0x -c myLib.cpp

请注意,您使用的编译器版本并未附带正常工作的正则表达式实现。您可以使用Boost.regex,它应该可以工作,而无需使用 C++11 选项进行编译。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-29
    • 1970-01-01
    • 2015-03-11
    • 1970-01-01
    • 1970-01-01
    • 2013-03-24
    相关资源
    最近更新 更多