我遵循了这个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
我试图谷歌但没有运气。如果问题太基础,请道歉。
答案 0 :(得分:0)
错误消息指出您需要使用选项-std=c++0x
进行编译。
g++ -std=c++0x -c myLib.cpp
请注意,您使用的编译器版本不附带正则表达式的正常工作实现。您可以使用Boost.regex,它应该可以工作而无需使用C ++ 11选项进行编译。