简体   繁体   中英

Regex Complilation Errors in c++

I am working on a project with someone and we have the exact same code however his code compiles while mine continues to throw an error. Here is my code:

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;
}

And here are the errors I am getting:

'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>)'

Since my partner's code compiled and we couldn't find anything different about my code I was wondering if anyone could shed some light on this situation.

Thanks for your time.

EDIT: I forgot to mention I have:

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

in my header file.

I did some more research, Michael, and it would appear that #include <regex> is only a placeholder until GCC version 4.9.0.

If you're on a Windows system (which I seem to remember you are), you're using GCC via MinGW, so you'll need to update to the latest version manually. I'm on 4.9.1, and that's why it works for me. You can download that from Sourceforge.

If you're on Linux, you should be able to update gcc and g++ via the repositories.

If you don't want to update, you can also use the Boost libraries . However, I recommend updating your compiler, mainly because it is just a generally good idea to be using the latest version of whatever compiler you're implementing, unless you have an explicit reason not to.

Sorry I couldn't answer that earlier, but I hope that helps!

In Windows following code works to remove Regex Complilation Errors.

#include <regex>
using namespace std;

Try using g++ as compiler. As it would not require to link libstdc++ explicitly. Whereas gcc, requires one to explicitly link libstdc++.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM