I need to use regular expression matching in my program. I decided to use boost library for it, but I receive strange fail when trying to compile. What is wrong??
There is my code:
...
#include <boost/regex.hpp>
...
using namespace boost;
...
map <string, double>::iterator container::find (string toFind)
{
iterator it;
for (it=mainMap.begin(); it!=mainMap.end(); it++)
{
regex e ((*it).first); //this line works correct
if ( regex_match (toFind, e ) )
return it;
}
return it;
}
...
Error message is to big for posting, there is its beginning:
tmp/cczkfDcy.o(.gnu.linkonce.t._ZN5boost11basic_regexIcNS_12regex_traitsIcEESaIcEED1Ev+0x11):
In function boost::basic_regex<char, boost::regex_traits<char>,
std::allocator<char> >::~basic_regex()': : undefined reference to
boost::reg_expression,
std::allocator >::~reg_expression()' ...
Add:
-lboost_regex
to your compiler options.
Related
This is my application:
// regex_example.cpp:
// How to compile:
// $ g++ -std=c++11 regex_example.cpp -o regex_example
#include <iostream>
#include <string>
#include <regex>
int main()
{
std::string input = "Pizza Carrot Lasagna 15000 ";
std::smatch match;
std::regex test_reg("[^0-9]*([0-9]+).*");
if (std::regex_search(input.begin(), input.end(), match, test_reg))
{
std::cout << "This is the string found: " << match[1] << std::endl;
}
return 0;
}
When I compile it, this is what the compiler shows me:
regex_example.cpp: In function ‘int main()’: regex_example.cpp:24:68:
error: no matching function for call to
‘regex_search(std::__cxx11::basic_string::iterator,
std::__cxx11::basic_string::iterator, std::__cxx11::smatch&,
std::__cxx11::regex&)’ if (std::regex_search(input.begin(),
input.end(), match, test_reg))
Basically, I'm trying to do the following:
1 - Have this compile. I don't understand why I'm getting the syntax error.
2 - From the input string, I'm trying to extract the number 15000. I'm assuming that when I get this compiling, I will get a string of 15000.
Using
std::regex_search(input.begin(), input.end(), match, test_reg)
Calls the overload
template< class BidirIt, class Alloc, class CharT, class Traits >
bool regex_search( BidirIt first, BidirIt last,
std::match_results<BidirIt,Alloc>& m,
const std::basic_regex<CharT,Traits>& e,
std::regex_constants::match_flag_type flags =
std::regex_constants::match_default );
and it requires that the match_result to be a std::match_results<BidirIt,Alloc>. In this case that is a std::string::iterator but match, which is a std::smatch, uses a std::string::const_iterator. Since those do not match, the compiler is unable to determine what BidirIt should deduce to.
There are many ways you can work around this but the simplest solution is to just use the std::string overload like:
std::regex_search(input, match, test_reg)
(Live Example)
Okay, very stumped on this one. Works with Visual Studio, but not Code::Blocks (GNU compiler).
transform(m_teams.begin(), m_teams.end(), inserter(teamNames, teamNames.end()),
[](stVecPair team) -> string { return team.first; });
m_teams is a map : typedef map<string, vector<Person*> > stVecMap;
teamNames is a set : typedef set<string> StrSet;
stVecPair is a pair matching m_teams : typedef pair<string, vector<Person*> > stVecPair;
Full Error
error: no matching function for call to 'transform(std::map<std::basic_string<char>,
std::vector<Person*> >::const_iterator, std::map<std::basic_string<char>,
std::vector<Person*> >::const_iterator, std::insert_iterator<std::set<std::basic_string<char> > >,
RaceAnalyzer::teams() const::<lambda(RaceAnalyzer::stVecPair&)>)
As I commented, you've forgotten -std=c++11 option >o<
I was using the topcoder C++ compiler, and although this code just run fine in Linux gcc, the topcoder compiler gave this error:
your code did not compile:
errors compiling:
Your class or method was improperly declared: In function
‘std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >,
std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > _wrapper::thunk(std::string)’:
Your class or method was improperly declared:20034:
error: conversion from ‘void’ to non-scalar type
‘std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >,
std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >’ requested
This is the code snippet where it is flagging error:
class BinaryCode {
public:
static int get_digit(char c)
{
return (c-'0');
}
void decode(string decd)
{
int i;
std::vector <int> decoded(decd.size());
std::transform(decd.begin(), decd.end(), decoded.begin(), &get_digit);
int length=decoded.size();
This is the topcoder problem description:
Definition Class:BinaryCode
Method:decode
Parameters:string
Returns:vector <string>
Method signature:
vector <string> decode(string message)
(be sure your method is public)
Your method signature is:
void decode(string decd)
Should be:
vector <string> decode(string message)
TopCoder compiles your code with testing code for the problem. Make sure the code you provide meets the requirements in the problem statement.
Topcoder compiler is expecting the function to be
vector <string> decode(string message)
while your function is
void decode(string message)
You are using 'void' instead of vector < string >
Try to use
using namespace std;
it fixed my problem. And also includes, it puts your code into separate file
#include <vector>
#include <string>
I have read some amount of documentation, and I am more familiar with the current version being shipped with VS2010. But for now I am stuck with ubuntu 8.04, and boost 1.34 and am getting some weird sort of error. Can anybody tell what I am doing wrong. Here is the man page for regex_search boost v1.34
Here is what I am doing in my code :
std::string sLine;
getline(dataFile, sLine);
boost::match_results<std::string::const_iterator> lineSmatch;
boost::match_flag_type regFlags = boost::match_default;
boost::regex finalRegex(linePattern);
boost::regex_search(sLine.begin(), sLine.end(), lineSmatch, finalRegex, regFlags);
Here is the compilation error:
error: no matching function for call to 'regex_search(__gnu_cxx::__normal_iterator, std::allocator > >, __gnu_cxx::__normal_iterator, std::allocator > >, boost::match_results<__gnu_cxx::__normal_iterator, std::allocator > >, std::allocator, std::allocator > > > > >&, boost::regex&, boost::regex_constants::match_flag_type&)'
If you are going to apply regex_search to sLine itself instead of
iterator range, as Howard answered, you can use sLine instead of
begin() and end().
For example:
boost::regex_search(sLine, lineSmatch, finalRegex, regFlags);
If you have to give iterator range to regex_search, since the type
argument for match_results is const_iterator, the first and second
arguments for regex_search need to be const_iterator too.
For example:
std::string::const_iterator b = sLine.begin(), e = sLine.end();
boost::regex_search(b, e, lineSmatch, finalRegex, regFlags);
Hope this helps
Can't help you specifically with ubuntu 8.04, and boost 1.34. However the following compiles for me on libc++ which implements C++11. Perhaps it is close enough to your environment to tell you what is wrong.
#include <regex>
#include <fstream>
int main()
{
std::ifstream dataFile;
std::string sLine, linePattern;
getline(dataFile, sLine);
std::match_results<std::string::const_iterator> lineSmatch;
std::regex_constants::match_flag_type regFlags =
std::regex_constants::match_default;
std::regex finalRegex(linePattern);
std::regex_search(sLine, lineSmatch, finalRegex, regFlags);
}
i have a string. I want to delete the last character of the string if it is a space.
i tried the following code,
str.erase(remove_if(str.begin(), str.end(), isspace), str.end());
but my g++ compiler gives me an error saying:
error: no matching function for call to ‘remove_if(__gnu_cxx::__normal_iterator<char*,
std::basic_string<char, std::char_traits<char>, std::allocator<char> > >,
__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>,
std::allocator<char> > >, <unresolved overloaded function type>)’
please help.
The first problem is that isspace has multiple overloads in the C++ Standard Library. The initial fix is to provide an explicit type for the function so that the compiler knows which function to take the address of:
#include <string>
#include <algorithm>
#include <cctype>
int main()
{
std::string str = "lol hi innit";
str.erase(std::remove_if(str.begin(), str.end(), (int(*)(int))isspace), str.end());
std::cout << str; // will output: "lolhiinnit"
}
It's a big ugly but, hey, this is C++.
Second, your code will remove all spaces in the string, which is not what you seem to want. Consider a simple if statement on the last character of the string:
#include <string>
#include <cassert>
int main()
{
std::string str = "lol hi innit ";
assert(!str.empty());
if (*str.rbegin() == ' ')
str.resize(str.length()-1);
std::cout << "[" << str << "]"; // will output: "[lol hi innit]"
}
Hope this helps.
I think it can't figure out what isspace is (according to the "unresolved overloaded function type" as a third parameter to remove_if in the error message). Try ::isspace, and include ctype.h
I had the exact same problem as you, so I got rid of isspace. I just went with this:
str.erase(std::remove_if(str.begin(),str.end(),' '),str.end());
That worked for me with Visual C++ 2012, using Visual Studio 2012. See if it works for you.
You are missing an std:: for remove_if