0

I'm new to c++; I have a simple code to check if a string a string is a palindrome:

#include <iostream>
#include <boost/regex.hpp>
#include <sstream>

using namespace std;
using namespace boost;

string PalindromeTwo(string str);

int main(){

  cout << PalindromeTwo("A war at Tarawa!");

}

string PalindromeTwo(string str) {

  int head = 0;
  int tail = str.size() - 1;
  boost::regex e("^[a-zA-Z]*$");
  stringstream stream;
  while(head <= tail){
    stream << str.at(head);
    string strchar = stream.str();
    stream.str(string());
    if(!boost::regex_match(strchar,e))
      head++;
    stream << str.at(tail);
    string strchar1 = stream.str();
    stream.str(string());
    if(!boost::regex_match(strchar1,e))
      tail--;
    if(strchar.compare(strchar1) != 0)
      return "false";
  }
  return "true";

}

I compiled this code with: g++ -o palin palin.cpp.

Then I got such a long error, I think it's just a little error, but c++ gave me such a long error and I couldn't find any clue from it, any experienced c++ developers could you tell me how to debug in c++ with such a long error?

/tmp/ccEwKTX0.o: In function `bool boost::regex_match<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > >, char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >(__gnu_cxx::__normal_iterator<char const*, std::string>, __gnu_cxx::__normal_iterator<char const*, std::string>, boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > > >&, boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags)':
palin.cpp:(.text._ZN5boost11regex_matchIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS5_EEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEbT_SD_RNS_13match_resultsISD_T0_EERKNS_11basic_regexIT1_T2_EENS_15regex_constants12_match_flagsE[_ZN5boost11regex_matchIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS5_EEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEbT_SD_RNS_13match_resultsISD_T0_EERKNS_11basic_regexIT1_T2_EENS_15regex_constants12_match_flagsE]+0x77): undefined reference to `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::match()'
/tmp/ccEwKTX0.o: In function `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::assign(char const*, char const*, unsigned int)':
palin.cpp:(.text._ZN5boost11basic_regexIcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE6assignEPKcS7_j[_ZN5boost11basic_regexIcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE6assignEPKcS7_j]+0x2a): undefined reference to `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::do_assign(char const*, char const*, unsigned int)'
/tmp/ccEwKTX0.o: In function `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::perl_matcher(__gnu_cxx::__normal_iterator<char const*, std::string>, __gnu_cxx::__normal_iterator<char const*, std::string>, boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > > >&, boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags, __gnu_cxx::__normal_iterator<char const*, std::string>)':
palin.cpp:(.text._ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEC2ES6_S6_RNS_13match_resultsIS6_S9_EERKNS_11basic_regexIcSD_EENS_15regex_constants12_match_flagsES6_[_ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEC5ES6_S6_RNS_13match_resultsIS6_S9_EERKNS_11basic_regexIcSD_EENS_15regex_constants12_match_flagsES6_]+0x113): undefined reference to `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::construct_init(boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags)'
collect2: error: ld returned 1 exit status
msn
  • 437
  • 1
  • 5
  • 11
  • What does that question content have to do with it's title? – πάντα ῥεῖ Feb 15 '15 at 20:07
  • @vsoftco could you tell me from which line of the error you know that it's a linker error? I'm learning a way, thank you. – msn Feb 15 '15 at 20:11
  • @msn `error: ld returned 1 exit status`. `ld` is the name of the linker. Every time you see it at the end of a long list of errors, it means you have a linker error. If you'd have had a compile time error, then the linker would not even be invoked, so definitely in your case the program passed compilation stage ok. – vsoftco Feb 15 '15 at 20:11

1 Answers1

2

This is not a compile error btw, it's a linker error, you are not linking with regex library of boost (unlike most of Boost, regex is a compiled library so you need to link it). Better, drop boost::regex and just use std::regex (#include <regex>) which is part of standard library in C++11 (compile with -std=c++11), and all pain will go away.

vsoftco
  • 55,410
  • 12
  • 139
  • 252
  • This is what I did before, but from [this](http://stackoverflow.com/questions/11269766/no-matches-with-c11-regex) post, it is said that `` is not supported, so I tried to use boost. – msn Feb 15 '15 at 20:21
  • @msn, indeed, `regex` *used* not to be implemented in the standard library, but now (starting I think with g++4.9) it is. clang++ also provides full support for it. Just test it and see if it works. If you really want to use `Boost` (or have an older compiler), then make sure you link with the library, like `-l/path/to/libregex`. Better, try to update your compiler to a C++11 compliant one. – vsoftco Feb 15 '15 at 20:23
  • I got `terminate called after throwing an instance of 'std::regex_error'` when running, is my regex pattern wrong? – msn Feb 15 '15 at 20:24
  • what compiler are you using? `g++ --version` Probably it's older, so then use Boost but link with the `regex` library, `g++ -lboost_regex -o palin palin.cpp`. – vsoftco Feb 15 '15 at 20:25
  • I got another error `/usr/bin/ld: cannot find -lboost_regex` with boost, my g++ version is `4.8.2` – msn Feb 15 '15 at 20:50
  • @msn then you probably did not compile `Boost`. Try searching online "how to compile with boost regex" – vsoftco Feb 15 '15 at 20:50
  • I've tried the solusion [here](http://stackoverflow.com/questions/8932380/g-cant-find-boost-libraries-i-say-theyre-in-plain-sight) and [here](http://stackoverflow.com/questions/21261958/g-cannot-find-boost-library), none of them works for me. – msn Feb 15 '15 at 20:51
  • What operating system are you using? And how did you install Boost? Make sure it is compiled, if not, compile the Boost library. You should have `libboost_system.so` installed somewhere in your file system. Use also `-L/path/to/boost/libs` to specify the path to the Boost libraries. – vsoftco Feb 15 '15 at 20:54
  • Now boost works, I'll try to fix g++ and make std::regex work, thanks for your help. – msn Feb 15 '15 at 20:56