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);
}
Related
I'm using templates while implementing AVL trees on Ubuntu.
The file will not compile when I write template class AVLTree<std::list<int> >;, it tells me:
undefined reference to `AVLTree < std::__cxx11::list < std::__cxx11::basic_string < char, std::char_traits < char>, std::allocator < char> >, std::allocator < std::__cxx11::basic_string < char, std::char_traits < char>, std::allocator < char> > > > >::insert(std::__cxx11::basic_string < char, std::char_traits < char>, std::allocator < char> >)'
And I don't get what it doesn't have the reference to.
But it compiles just fine when I write template class AVLTree<std::list<string> >;
I need to let AVLTree store linked lists that store string values.
Why does one compile and the other doesn't? How to solve my problem?
PS: I've included <list>, <string>, and <iostream>, along with my own header file.
Examining the error message closely shows that linker cannot find the AVLTree::insert(string) method.
Based on the sparse information that you posted, my best hypothesis is that you changed the template parameter in the following line from list<string> to list<int>:
template class AVLTree<std::list<string>>;
This line of code explicitly tells the compiler to instantiate a version of the AVLTree template using list<string> as the template parameter. Thus, when you try to compile the code after the change it gives you the error message that it cannot find the AVLTree::insert(string) function because the compiler is now generating the code for list<int> instead.
Your program contains other code that is referencing AVLTree<list<string>>. At a minimum you will have to update that code to use list<int> as well.
Plus, if you simplify the problem down to something you can post the code for on this site, then you will either find the issue during that process or at least have a change of getting a good answer.
According to cppreference, C++11 should support:
template< class InputIt >
iterator insert( const_iterator pos, InputIt first, InputIt last );
But when I try to compile following code using g++ 4.9.2:
std::string str{ "hello world" }, addition{ "h my" };
auto iter = str.erase(str.begin(), str.begin() + 4);
iter = str.insert(next(iter), addition.begin(), addition.end()); // Error
I receive the following error (live example):
error: no match for 'operator=' (operand types are '__gnu_cxx::__normal_iterator<char*, std::basic_string<char> >' and 'void')
iter = str.insert(next(iter), addition.begin(), addition.end());
^
However, Visual Studio 2013 and Clang seem no problem.
gcc used a non-conforming copy-on-write (COW) implementation in 4.9.2 they changed this in 5.x series and we can see from a live godbolt session that this is broken in 4.9.2 but works in 5.1.
This change is documented in the GCC 5 Release Series Release notes:
A new implementation of std::string is enabled by default, using the small string optimization instead of copy-on-write reference counting.
We can find a description of the difference between the COW and SSO version from the [libstdc++ mailing list: New std::string implementation (https://gcc.gnu.org/ml/gcc-patches/2014-11/msg01785.html):
This is the long-awaited ABI break for std::string, replacing our venerable Copy-On-Write implementation with a C++11-conforming Small-String-Optimization implementation (based on Paolo's vstring).
The gist of it is adding a second complete std::string implementation that is tagged with abi_tag("cxx11") so it mangles differently (as already done for std::list and std::ios_base::failure).
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 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.
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