Ошибка использования регулярного выражения в пользовательской библиотеке

0

Я следил за этой ссылкой, и все было хорошо. Теперь я попытался разместить эту функцию 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

Я пытаюсь сделать это, но не повезло. Извините, если вопросы слишком просты.

  • 0
    Попробуйте int myLib::rreplace(char *buf, int size, regex_t *re, char *rp){}
  • 0
    попробовал, не сработало.
Показать ещё 1 комментарий
Теги:
compiler-errors

1 ответ

0
Лучший ответ

В сообщении об ошибке указано, что вам нужно скомпилировать с опцией -std=c++0x.

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

Обратите внимание, что версия компилятора, которую вы используете, не поставляется с правильно работающей реализацией регулярного выражения. Вы можете использовать Boost.regex, который должен работать, не требуя компиляции с параметром С++ 11.

Ещё вопросы

Сообщество Overcoder
Наверх
Меню