c ++中的正则表达式编译错误

时间:2014-11-14 02:16:09

标签: c++ regex compilation undefined-reference

我正在与某人合作开发一个项目,但我们的代码完全相同,但是我的代码会编译,而我的代码会继续抛出错误。这是我的代码:

bool r_parser::parseMake(string name, string command, string type, string val)
{
    regex regName("^\\w+$");
    if(std::regex_match(name, regName) == true)
    {
        cout << "We have a match!" << endl;
    }
    return false;
}

以下是我得到的错误:

'undefined reference to std::basic_regex<char, std::regex_traits<char> >::_M_compile()'

'undefined reference to bool std::regex_match<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<std::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > >, char, std::regex_traits<char> >(__gnu_cxx::__normal_iterator<char const*, std::string>, __gnu_cxx::__normal_iterator<char const*, std::string>, std::match_results<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<std::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > > >&, std::basic_regex<char, std::regex_traits<char> > const&, std::bitset<11u>)'

由于我的合作伙伴的代码已编译,我们无法找到与我的代码有任何不同之处,我想知道是否有人可以了解这种情况。

感谢您的时间。

编辑:我忘了提及我:

#include <regex>
#include <string>
#include <iostream>
using namespace std;

在我的头文件中。

3 个答案:

答案 0 :(得分:3)

我做了一些更多的研究,Michael,看来#include <regex>only a placeholder until GCC version 4.9.0.

如果您使用的是Windows系统(我似乎记得你是),那么您通过MinGW使用GCC,因此您需要手动更新到最新版本。我在4.9.1,这就是为什么它对我有用。 You can download that from Sourceforge.

如果您使用的是Linux,那么您应该可以通过存储库更新gcc和g ++。

如果您不想更新,也可以使用the Boost libraries。但是,我建议更新你的编译器,主要是因为使用你正在实现的任何编译器的最新版本通常是一个好主意,除非你有明确的理由不这样做。

抱歉,我之前无法回答,但我希望有所帮助!

答案 1 :(得分:1)

在Windows中,以下代码可用于删除Regex Complilation Errors。

#include <regex>
using namespace std;

答案 2 :(得分:0)

尝试使用g ++作为编译器。因为它不需要明确地链接libstdc ++。 而gcc,需要一个显式链接libstdc ++。