Why boost::replace_all_copy is not working? C++ - c++

I am trying to make working code to replace text of line. Using boost_1_64_0 .
getline (ifs, line);
cout << line << endl;
// 1.
boost::replace_all(line, "abc", "hij");
boost::replace_all(line, "def", "klm");
// 2.
line = boost::replace_all_copy
boost::replace_all_copy<string>
(
("abc def abc def", "abc", "hij")
, "def"
, "klm"
);
I am getting these errors:
main.cpp||In function 'int main()':|
main.cpp|37|error: no match for 'operator=' in 'line = boost::algorithm::replace_all_copy'|
main.cpp|37|note: candidates are:|
\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.h|543|note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string<char>]|
mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.h|543|note: no known conversion for argument 1 from '<unresolved overloaded function type>' to 'const std::basic_string<char>&'|
mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.h|551|note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const _CharT*) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string<char>]|
mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.h|551|note: no known conversion for argument 1 from '<unresolved overloaded function type>' to 'const char*'|
mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.h|562|note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string<char>]|
mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.h|562|note: no known conversion for argument 1 from '<unresolved overloaded function type>' to 'char'|
main.cpp|38|error: expected ';' before 'boost'|
main.cpp|53|error: expected '}' before 'else'|
Why the error happens or how can I correct them?

You have written obviously broken code:
line = boost::replace_all_copy boost::replace_all_copy<string>(...)

Related

Need help understanding why this invalid C++ code compiles successfully

The following code compile correctly for me in some environments (e.g. on compiler explorer with GCC 9.3.0) and complains in others (CentOS 7.9.2009 (Core), GCC 9.3.1).
#include <iostream>
#include <string>
using namespace std;
int main() {
std::string name="aakash";
name.erase(name.begin()+2, name.cend());
return 0;
}
When I get an error, the error is:
test.cpp:6:40: error: no matching function for call to 'std::basic_string<char>::erase(__gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, std::basic_string<char>::const_iterator)'
6 | name.erase(name.begin()+2, name.cend());
| ^
In file included from /opt/rh/devtoolset-9/root/usr/include/c++/9/string:55,
from /opt/rh/devtoolset-9/root/usr/include/c++/9/bits/locale_classes.h:40,
from /opt/rh/devtoolset-9/root/usr/include/c++/9/bits/ios_base.h:41,
from /opt/rh/devtoolset-9/root/usr/include/c++/9/ios:42,
from /opt/rh/devtoolset-9/root/usr/include/c++/9/ostream:38,
from /opt/rh/devtoolset-9/root/usr/include/c++/9/iostream:39,
from test.cpp:1:
/opt/rh/devtoolset-9/root/usr/include/c++/9/bits/basic_string.h:4698:7: note: candidate: 'std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::erase(std::basic_string<_CharT, _Traits, _Alloc>::size_type, std::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]'
4698 | erase(size_type __pos = 0, size_type __n = npos)
| ^~~~~
/opt/rh/devtoolset-9/root/usr/include/c++/9/bits/basic_string.h:4698:23: note: no known conversion for argument 1 from '__gnu_cxx::__normal_iterator<char*, std::basic_string<char> >' to 'std::basic_string<char>::size_type' {aka 'long unsigned int'}
4698 | erase(size_type __pos = 0, size_type __n = npos)
| ~~~~~~~~~~^~~~~~~~~
/opt/rh/devtoolset-9/root/usr/include/c++/9/bits/basic_string.h:4714:7: note: candidate: 'std::basic_string<_CharT, _Traits, _Alloc>::iterator std::basic_string<_CharT, _Traits, _Alloc>::erase(std::basic_string<_CharT, _Traits, _Alloc>::iterator) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::basic_string<_CharT, _Traits, _Alloc>::iterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >; typename _Alloc::rebind<_CharT>::other::pointer = char*]'
4714 | erase(iterator __position)
| ^~~~~
/opt/rh/devtoolset-9/root/usr/include/c++/9/bits/basic_string.h:4714:7: note: candidate expects 1 argument, 2 provided
/opt/rh/devtoolset-9/root/usr/include/c++/9/bits/basic_string.h:4734:7: note: candidate: 'std::basic_string<_CharT, _Traits, _Alloc>::iterator std::basic_string<_CharT, _Traits, _Alloc>::erase(std::basic_string<_CharT, _Traits, _Alloc>::iterator, std::basic_string<_CharT, _Traits, _Alloc>::iterator) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::basic_string<_CharT, _Traits, _Alloc>::iterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >; typename _Alloc::rebind<_CharT>::other::pointer = char*]'
4734 | erase(iterator __first, iterator __last);
| ^~~~~
/opt/rh/devtoolset-9/root/usr/include/c++/9/bits/basic_string.h:4734:40: note: no known conversion for argument 2 from '__normal_iterator<const char*,[...]>' to '__normal_iterator<char*,[...]>'
4734 | erase(iterator __first, iterator __last);
| ~~~~~~~~~^~~~~~
| ~~~~~~~~~^~~~~~
The error looks reasonable to me because, as per C++ documentation, both arguments to std::basic_string::erase should either be of type iterator or const_iterator.
So, when it works (e.g. here), what allows it to work?
Since C++11, both arguments to the two-iterator overload of std::string::erase() are const_iterator. See form #3 on this cppreference page (the one you linked in your question).
Further, the C++11 Standard requires that a conatiner's iterator types are convertible to the equivalent const_iterator.
From this Draft C++11 Standard, in §23.2.1 [container.requirements.general], Table 96 has the following entry (bold emphasis mine):
Expression
Return Type
Operational Semantics
Assertion/note
…
X::iterator
iterator type whose value type is T
any iterator category that meets the forward iterator requirements. convertible to X::const_iterator.
…
So, in your call to .erase(), the first argument is being converted to a string::const_iterator.
Thus, the bug is in those compilers that don't accept your call, assuming you have set them also to use the C++11 (or later) standard.

Odd compile errors on c++ program

So i'm trying to make a program that reads input from the user, transforms it into a note like this:
--- time
note
Here's the code for it:
#include <iostream>
#include <fstream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main () {
string notesvar{"/notes"};
string home{std::getenv("HOME")};
string notes{home, notesvar};
time_t now = time(0);
char* dt = ctime(&now);
ofstream myfile;
std::cout << ": ";
string x{};
std::cin >> x;
myfile.open ("placeholder", ios_base::app);
myfile << "---- " << dt;
myfile << x << '\n';
myfile << "\n";
myfile.close();
return 0;
}
This compiles with a lot of notes, but here's the compile log:
notes.cpp: In function ‘int main()’:
notes.cpp:9:32: error: no matching function for call to ‘std::__cxx11::basic_string<char>::basic_string(<brace-enclosed initializer list>)’
9 | string notes{home, notesvar};
| ^
In file included from /usr/include/c++/9.3.0/string:55,
from /usr/include/c++/9.3.0/bits/locale_classes.h:40,
from /usr/include/c++/9.3.0/bits/ios_base.h:41,
from /usr/include/c++/9.3.0/ios:42,
from /usr/include/c++/9.3.0/ostream:38,
from /usr/include/c++/9.3.0/iostream:39,
from notes.cpp:1:
/usr/include/c++/9.3.0/bits/basic_string.h:650:2: note: candidate: ‘template<class _Tp, class> std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _Tp&, const _Alloc&)’
650 | basic_string(const _Tp& __t, const _Alloc& __a = _Alloc())
| ^~~~~~~~~~~~
/usr/include/c++/9.3.0/bits/basic_string.h:650:2: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/9.3.0/bits/move.h:55,
from /usr/include/c++/9.3.0/bits/nested_exception.h:40,
from /usr/include/c++/9.3.0/exception:144,
from /usr/include/c++/9.3.0/ios:39,
from /usr/include/c++/9.3.0/ostream:38,
from /usr/include/c++/9.3.0/iostream:39,
from notes.cpp:1:
/usr/include/c++/9.3.0/type_traits: In substitution of ‘template<bool _Cond, class _Tp> using enable_if_t = typename std::enable_if::type [with bool _Cond = std::integral_constant<bool, false>::value; _Tp = void]’:
/usr/include/c++/9.3.0/bits/basic_string.h:117:8: required by substitution of ‘template<class _CharT, class _Traits, class _Alloc> template<class _Tp, class _Res> using _If_sv = std::enable_if_t<std::__and_<std::is_convertible<const _Tp&, std::basic_string_view<_CharT, _Traits> >, std::__not_<std::is_convertible<const _Tp*, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>*> >, std::__not_<std::is_convertible<const _Tp&, const _CharT*> > >::value, _Res> [with _Tp = std::__cxx11::basic_string<char>; _Res = void; _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
/usr/include/c++/9.3.0/bits/basic_string.h:648:30: required from here
/usr/include/c++/9.3.0/type_traits:2384:11: error: no type named ‘type’ in ‘struct std::enable_if<false, void>’
2384 | using enable_if_t = typename enable_if<_Cond, _Tp>::type;
| ^~~~~~~~~~~
In file included from /usr/include/c++/9.3.0/string:55,
from /usr/include/c++/9.3.0/bits/locale_classes.h:40,
from /usr/include/c++/9.3.0/bits/ios_base.h:41,
from /usr/include/c++/9.3.0/ios:42,
from /usr/include/c++/9.3.0/ostream:38,
from /usr/include/c++/9.3.0/iostream:39,
from notes.cpp:1:
/usr/include/c++/9.3.0/bits/basic_string.h:639:2: note: candidate: ‘template<class _Tp, class> std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _Tp&, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, const _Alloc&)’
639 | basic_string(const _Tp& __t, size_type __pos, size_type __n,
| ^~~~~~~~~~~~
/usr/include/c++/9.3.0/bits/basic_string.h:639:2: note: template argument deduction/substitution failed:
notes.cpp:9:32: note: candidate expects 4 arguments, 2 provided
9 | string notes{home, notesvar};
| ^
In file included from /usr/include/c++/9.3.0/string:55,
from /usr/include/c++/9.3.0/bits/locale_classes.h:40,
from /usr/include/c++/9.3.0/bits/ios_base.h:41,
from /usr/include/c++/9.3.0/ios:42,
from /usr/include/c++/9.3.0/ostream:38,
from /usr/include/c++/9.3.0/iostream:39,
from notes.cpp:1:
/usr/include/c++/9.3.0/bits/basic_string.h:625:9: note: candidate: ‘template<class _InputIterator, class> std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(_InputIterator, _InputIterator, const _Alloc&)’
625 | basic_string(_InputIterator __beg, _InputIterator __end,
| ^~~~~~~~~~~~
/usr/include/c++/9.3.0/bits/basic_string.h:625:9: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/9.3.0/bits/stl_algobase.h:65,
from /usr/include/c++/9.3.0/bits/char_traits.h:39,
from /usr/include/c++/9.3.0/ios:40,
from /usr/include/c++/9.3.0/ostream:38,
from /usr/include/c++/9.3.0/iostream:39,
from notes.cpp:1:
/usr/include/c++/9.3.0/bits/stl_iterator_base_types.h: In substitution of ‘template<class _InIter> using _RequireInputIter = typename std::enable_if<std::is_convertible<typename std::iterator_traits<_Iterator>::iterator_category, std::input_iterator_tag>::value>::type [with _InIter = std::__cxx11::basic_string<char>]’:
/usr/include/c++/9.3.0/bits/basic_string.h:621:9: required from here
/usr/include/c++/9.3.0/bits/stl_iterator_base_types.h:232:11: error: no type named ‘iterator_category’ in ‘struct std::iterator_traits<std::__cxx11::basic_string<char> >’
232 | using _RequireInputIter = typename
| ^~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/9.3.0/string:55,
from /usr/include/c++/9.3.0/bits/locale_classes.h:40,
from /usr/include/c++/9.3.0/bits/ios_base.h:41,
from /usr/include/c++/9.3.0/ios:42,
from /usr/include/c++/9.3.0/ostream:38,
from /usr/include/c++/9.3.0/iostream:39,
from notes.cpp:1:
/usr/include/c++/9.3.0/bits/basic_string.h:587:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
587 | basic_string(basic_string&& __str, const _Alloc& __a)
| ^~~~~~~~~~~~
/usr/include/c++/9.3.0/bits/basic_string.h:587:56: note: no known conversion for argument 2 from ‘std::string’ {aka ‘std::__cxx11::basic_string<char>’} to ‘const std::allocator<char>&’
587 | basic_string(basic_string&& __str, const _Alloc& __a)
| ~~~~~~~~~~~~~~^~~
/usr/include/c++/9.3.0/bits/basic_string.h:583:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
583 | basic_string(const basic_string& __str, const _Alloc& __a)
| ^~~~~~~~~~~~
/usr/include/c++/9.3.0/bits/basic_string.h:583:61: note: no known conversion for argument 2 from ‘std::string’ {aka ‘std::__cxx11::basic_string<char>’} to ‘const std::allocator<char>&’
583 | basic_string(const basic_string& __str, const _Alloc& __a)
| ~~~~~~~~~~~~~~^~~
/usr/include/c++/9.3.0/bits/basic_string.h:579:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(std::initializer_list<_Tp>, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
579 | basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc())
| ^~~~~~~~~~~~
/usr/include/c++/9.3.0/bits/basic_string.h:579:45: note: no known conversion for argument 1 from ‘std::string’ {aka ‘std::__cxx11::basic_string<char>’} to ‘std::initializer_list<char>’
579 | basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc())
| ~~~~~~~~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/9.3.0/bits/basic_string.h:552:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
552 | basic_string(basic_string&& __str) noexcept
| ^~~~~~~~~~~~
/usr/include/c++/9.3.0/bits/basic_string.h:552:7: note: candidate expects 1 argument, 2 provided
/usr/include/c++/9.3.0/bits/basic_string.h:540:7: note: candidate: ‘template<class> std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, _CharT, const _Alloc&)’
540 | basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc())
| ^~~~~~~~~~~~
/usr/include/c++/9.3.0/bits/basic_string.h:540:7: note: template argument deduction/substitution failed:
notes.cpp:9:18: note: cannot convert ‘home’ (type ‘std::string’ {aka ‘std::__cxx11::basic_string<char>’}) to type ‘std::__cxx11::basic_string<char>::size_type’ {aka ‘long unsigned int’}
9 | string notes{home, notesvar};
| ^~~~
In file included from /usr/include/c++/9.3.0/string:55,
from /usr/include/c++/9.3.0/bits/locale_classes.h:40,
from /usr/include/c++/9.3.0/bits/ios_base.h:41,
from /usr/include/c++/9.3.0/ios:42,
from /usr/include/c++/9.3.0/ostream:38,
from /usr/include/c++/9.3.0/iostream:39,
from notes.cpp:1:
/usr/include/c++/9.3.0/bits/basic_string.h:525:7: note: candidate: ‘template<class> std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&)’
525 | basic_string(const _CharT* __s, const _Alloc& __a = _Alloc())
| ^~~~~~~~~~~~
/usr/include/c++/9.3.0/bits/basic_string.h:525:7: note: template argument deduction/substitution failed:
notes.cpp:9:18: note: cannot convert ‘home’ (type ‘std::string’ {aka ‘std::__cxx11::basic_string<char>’}) to type ‘const char*’
9 | string notes{home, notesvar};
| ^~~~
In file included from /usr/include/c++/9.3.0/string:55,
from /usr/include/c++/9.3.0/bits/locale_classes.h:40,
from /usr/include/c++/9.3.0/bits/ios_base.h:41,
from /usr/include/c++/9.3.0/ios:42,
from /usr/include/c++/9.3.0/ostream:38,
from /usr/include/c++/9.3.0/iostream:39,
from notes.cpp:1:
/usr/include/c++/9.3.0/bits/basic_string.h:510:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]’
510 | basic_string(const _CharT* __s, size_type __n,
| ^~~~~~~~~~~~
/usr/include/c++/9.3.0/bits/basic_string.h:510:34: note: no known conversion for argument 1 from ‘std::string’ {aka ‘std::__cxx11::basic_string<char>’} to ‘const char*’
510 | basic_string(const _CharT* __s, size_type __n,
| ~~~~~~~~~~~~~~^~~
/usr/include/c++/9.3.0/bits/basic_string.h:492:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]’
492 | basic_string(const basic_string& __str, size_type __pos,
| ^~~~~~~~~~~~
/usr/include/c++/9.3.0/bits/basic_string.h:492:7: note: candidate expects 4 arguments, 2 provided
/usr/include/c++/9.3.0/bits/basic_string.h:476:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]’
476 | basic_string(const basic_string& __str, size_type __pos,
| ^~~~~~~~~~~~
/usr/include/c++/9.3.0/bits/basic_string.h:476:7: note: candidate expects 3 arguments, 2 provided
/usr/include/c++/9.3.0/bits/basic_string.h:461:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]’
461 | basic_string(const basic_string& __str, size_type __pos,
| ^~~~~~~~~~~~
/usr/include/c++/9.3.0/bits/basic_string.h:461:57: note: no known conversion for argument 2 from ‘std::string’ {aka ‘std::__cxx11::basic_string<char>’} to ‘std::__cxx11::basic_string<char>::size_type’ {aka ‘long unsigned int’}
461 | basic_string(const basic_string& __str, size_type __pos,
| ~~~~~~~~~~^~~~~
/usr/include/c++/9.3.0/bits/basic_string.h:448:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
448 | basic_string(const basic_string& __str)
| ^~~~~~~~~~~~
/usr/include/c++/9.3.0/bits/basic_string.h:448:7: note: candidate expects 1 argument, 2 provided
/usr/include/c++/9.3.0/bits/basic_string.h:440:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
440 | basic_string(const _Alloc& __a) _GLIBCXX_NOEXCEPT
| ^~~~~~~~~~~~
/usr/include/c++/9.3.0/bits/basic_string.h:440:7: note: candidate expects 1 argument, 2 provided
/usr/include/c++/9.3.0/bits/basic_string.h:431:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string() [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
431 | basic_string()
| ^~~~~~~~~~~~
/usr/include/c++/9.3.0/bits/basic_string.h:431:7: note: candidate expects 0 arguments, 2 provided
/usr/include/c++/9.3.0/bits/basic_string.h:145:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::__sv_wrapper, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
145 | basic_string(__sv_wrapper __svw, const _Alloc& __a)
| ^~~~~~~~~~~~
/usr/include/c++/9.3.0/bits/basic_string.h:145:33: note: no known conversion for argument 1 from ‘std::string’ {aka ‘std::__cxx11::basic_string<char>’} to ‘std::__cxx11::basic_string<char>::__svT_wrapper’
145 | basic_string(__sv_wrapper __svw, const _Alloc& __a)
|
It was compiled using g++ -std=c++17 -o notes notes.cpp. Sorry if this is a stupid question. I'm just beginning with C++.
What confuses me is the error on... line 232?! Thanks in advance for helping!
string notes{home, notesvar};
There is no std::string(std::string, std::string) constructor. To construct another string as concatenation of two other strings, just operator + add them.
string notes = home + notesvar;

error while working with boost::sregex_token_iterator

I want to search for a regular expression and print it with a color. I used boost::sregex_token_iterator to do this. this is my code
boost::regex re("ab.");
string s="";
string buf;
string infile("retest.txt");
//string color="green";
ifstream in(infile.c_str());
int lcount=0;
while (getline(in,buf))
{
boost::sregex_token_iterator p(buf.begin(), buf.end(), re, 0);
boost::sregex_token_iterator end;
lcount++;
cout <<"line : "<<lcount<<endl;
for (;p != end;++p)
{
string m(p->first, p->second);
cout<< m <<endl;
//cout <<*(p->first)<<endl;
//cout <<*(p->second)<<endl;
//unsigned int pos = buf.find(m);
buf = buf.insert(p->first,"\e[0;32m");
buf = buf.insert(p->second+m.length()+7,"\e[0m");
}
cout<<"\n";
s.append(buf);
s.append("\n");
}
in.close();
cout <<"s is: "<<s<<endl;
return 0;
}
but I get this error :
In file included from /usr/include/boost/config.hpp:35:0,
from /usr/include/boost/regex/config.hpp:53,
from /usr/include/boost/regex.hpp:28,
from main3.cc:3:
/usr/include/boost/config/compiler/gcc.hpp:92:7: warning: #warning "Unknown compiler version - please run the configure tests and report the results" [-Wcpp]
main3.cc: In function âint main(int, char**)â:
main3.cc:36:40: error: no matching function for call to âstd::basic_string::insert(const __gnu_cxx::__normal_iterator >&, const char [8])â
main3.cc:36:40: note: candidates are:
In file included from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/string:54:0,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/locale_classes.h:42,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/ios_base.h:43,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ios:43,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ostream:40,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/iostream:40,
from main3.cc:1:
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1182:7: note: void std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::iterator, std::basic_string<_CharT, _Traits, _Alloc>::size_type, _CharT) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc>::iterator = __gnu_cxx::__normal_iterator >; typename _Alloc::rebind<_CharT>::other::pointer = char*; std::basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int]
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1182:7: note: candidate expects 3 arguments, 2 provided
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1199:9: note: template void std::basic_string::insert(std::basic_string<_CharT, _Traits, _Alloc>::iterator, _InputIterator, _InputIterator) [with _InputIterator = _InputIterator; _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator]
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1199:9: note: template argument deduction/substitution failed:
main3.cc:36:40: note: cannot convert âp.boost::regex_token_iterator::operator-><__gnu_cxx::__normal_iterator >, char, boost::regex_traits, std::allocator >()->boost::sub_match<__gnu_cxx::__normal_iterator > >::.std::pair<__gnu_cxx::__normal_iterator >, __gnu_cxx::__normal_iterator > >::firstâ (type âconst __gnu_cxx::__normal_iterator >â) to type âstd::basic_string::iterator {aka __gnu_cxx::__normal_iterator >}â
In file included from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/string:54:0,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/locale_classes.h:42,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/ios_base.h:43,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ios:43,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ostream:40,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/iostream:40,
from main3.cc:1:
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1230:7: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::size_type, const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string; std::basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int]
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1230:7: note: no known conversion for argument 1 from âconst __gnu_cxx::__normal_iterator >â to âstd::basic_string::size_type {aka unsigned int}â
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1252:7: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::size_type, const std::basic_string<_CharT, _Traits, _Alloc>&, std::basic_string<_CharT, _Traits, _Alloc>::size_type, std::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string; std::basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int]
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1252:7: note: candidate expects 4 arguments, 2 provided
In file included from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/string:55:0,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/locale_classes.h:42,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/ios_base.h:43,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ios:43,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ostream:40,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/iostream:40,
from main3.cc:1:
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.tcc:361:6: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::size_type, const _CharT*, std::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int]
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.tcc:361:6: note: candidate expects 3 arguments, 2 provided
In file included from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/string:54:0,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/locale_classes.h:42,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/ios_base.h:43,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ios:43,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ostream:40,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/iostream:40,
from main3.cc:1:
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1293:7: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::size_type, const _CharT*) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string; std::basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int]
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1293:7: note: no known conversion for argument 1 from âconst __gnu_cxx::__normal_iterator >â to âstd::basic_string::size_type {aka unsigned int}â
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1316:7: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::size_type, std::basic_string<_CharT, _Traits, _Alloc>::size_type, _CharT) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string; std::basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int]
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1316:7: note: candidate expects 3 arguments, 2 provided
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1334:7: note: std::basic_string<_CharT, _Traits, _Alloc>::iterator std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::iterator, _CharT) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc>::iterator = __gnu_cxx::__normal_iterator >; typename _Alloc::rebind<_CharT>::other::pointer = char*]
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1334:7: note: no known conversion for argument 1 from âconst __gnu_cxx::__normal_iterator >â to âstd::basic_string::iterator {aka __gnu_cxx::__normal_iterator >}â
main3.cc:37:51: error: no matching function for call to âstd::basic_string::insert(__gnu_cxx::__normal_iterator >, const char [5])â
main3.cc:37:51: note: candidates are:
In file included from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/string:54:0,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/locale_classes.h:42,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/ios_base.h:43,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ios:43,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ostream:40,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/iostream:40,
from main3.cc:1:
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1182:7: note: void std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::iterator, std::basic_string<_CharT, _Traits, _Alloc>::size_type, _CharT) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc>::iterator = __gnu_cxx::__normal_iterator >; typename _Alloc::rebind<_CharT>::other::pointer = char*; std::basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int]
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1182:7: note: candidate expects 3 arguments, 2 provided
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1199:9: note: template void std::basic_string::insert(std::basic_string<_CharT, _Traits, _Alloc>::iterator, _InputIterator, _InputIterator) [with _InputIterator = _InputIterator; _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator]
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1199:9: note: template argument deduction/substitution failed:
main3.cc:37:51: note: cannot convert âp.boost::regex_token_iterator::operator-><__gnu_cxx::__normal_iterator >, char, boost::regex_traits, std::allocator >()->boost::sub_match<__gnu_cxx::__normal_iterator > >::.std::pair<__gnu_cxx::__normal_iterator >, __gnu_cxx::__normal_iterator > >::second.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator+ >((* &((__gnu_cxx::__normal_iterator >::difference_type)m.std::basic_string<_CharT, _Traits, Alloc>::length, std::allocator >())))._gnu_cxx::__normal_iterator<_Iterator, _Container>::operator+ >((* &7))â (type â__gnu_cxx::__normal_iterator >â) to type âstd::basic_string::iterator {aka __gnu_cxx::__normal_iterator >}â
In file included from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/string:54:0,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/locale_classes.h:42,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/ios_base.h:43,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ios:43,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ostream:40,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/iostream:40,
from main3.cc:1:
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1230:7: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::size_type, const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string; std::basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int]
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1230:7: note: no known conversion for argument 1 from â__gnu_cxx::__normal_iterator >â to âstd::basic_string::size_type {aka unsigned int}â
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1252:7: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::size_type, const std::basic_string<_CharT, _Traits, _Alloc>&, std::basic_string<_CharT, _Traits, _Alloc>::size_type, std::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string; std::basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int]
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1252:7: note: candidate expects 4 arguments, 2 provided
In file included from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/string:55:0,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/locale_classes.h:42,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/ios_base.h:43,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ios:43,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ostream:40,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/iostream:40,
from main3.cc:1:
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.tcc:361:6: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::size_type, const _CharT*, std::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int]
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.tcc:361:6: note: candidate expects 3 arguments, 2 provided
In file included from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/string:54:0,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/locale_classes.h:42,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/ios_base.h:43,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ios:43,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ostream:40,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/iostream:40,
from main3.cc:1:
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1293:7: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::size_type, const _CharT*) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string; std::basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int]
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1293:7: note: no known conversion for argument 1 from â__gnu_cxx::__normal_iterator >â to âstd::basic_string::size_type {aka unsigned int}â
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1316:7: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::size_type, std::basic_string<_CharT, _Traits, _Alloc>::size_type, _CharT) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string; std::basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int]
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1316:7: note: candidate expects 3 arguments, 2 provided
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1334:7: note: std::basic_string<_CharT, _Traits, _Alloc>::iterator std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::iterator, _CharT) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc>::iterator = __gnu_cxx::__normal_iterator >; typename _Alloc::rebind<_CharT>::other::pointer = char*]
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1334:7: note: no known conversion for argument 1 from â__gnu_cxx::__normal_iterator >â to âstd::basic_string::iterator {aka __gnu_cxx::__normal_iterator >}â
I know that the type of p->first is not the one which insert function accepts. but I don't know how to convert these to each other.
The issue might be your usage of std::string::insert. I can't see an insert(iterator, string) version among it's numerous overloads.
You might want to call desired version by explicitly stating length of char array:
void insert (iterator p, size_t n, char c)
The problem seems to be that boost is configured for use with a different version of the compiler rather than the one you are currently using.
Could you add the details of how you compiled it as well?

Using push_back on a vector of strings - C++

I am trying to use push_back on a vector of strings in C++. How can I push a single character on to the vector? Currently, I have tried the following, all without success:
Initialized a string (tried to) with the character.
Code
string str(main_string[0]);
vector_string.push_back(str);
Tried to invoke strcpy and thus copy contents. Const-ness seems to get in the way.
Code
string str;
strcpy(main_string[0], str.c_str());
vector_string.push_back(str);
Any more suggestions/ideas are most welcome.
Edit: The error logs are as follows:
test_push.C: In function ‘void test_push(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> > > >&, std::string)’:
test_push.C:50: error: no matching function for call to ‘std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char&)’
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.tcc:220: note: candidates are: std::basic_string<_CharT, _Traits, _Alloc>::basic_string(typename _Alloc::rebind<_CharT>::other::size_type, _CharT, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.tcc:213: note: std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>] <near match>
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.tcc:206: note: std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, typename _Alloc::rebind<_CharT>::other::size_type, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.tcc:194: note: std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::basic_string<_CharT, _Traits, _Alloc>&, typename _Alloc::rebind<_CharT>::other::size_type, typename _Alloc::rebind<_CharT>::other::size_type, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.tcc:184: note: std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::basic_string<_CharT, _Traits, _Alloc>&, typename _Alloc::rebind<_CharT>::other::size_type, typename _Alloc::rebind<_CharT>::other::size_type) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.tcc:170: note: std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.tcc:178: note: std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.h:2147: note: std::basic_string<_CharT, _Traits, _Alloc>::basic_string() [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
test_push.C:61: error: conversion from ‘char’ to non-scalar type ‘std::string’ requested
test_push:90: error: no matching function for call to ‘std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char&)’
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.tcc:220: note: candidates are: std::basic_string<_CharT, _Traits, _Alloc>::basic_string(typename _Alloc::rebind<_CharT>::other::size_type, _CharT, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.tcc:213: note: std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>] <near match>
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.tcc:206: note: std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, typename _Alloc::rebind<_CharT>::other::size_type, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
More info: In this function, either the entire string or just the first letter is pushed in to the vector based on its contents. So, in a way, I am looking for some function that converts a char into a C++ style std::string (to make it acceptable to a vector).
Thanks,
Sriram
You can also initialize with one character like this.
string str(1,main_string[0]);
vector_string.push_back(str);
You can add the character directly via an appropriate string constructor:
vector_string.push_back(string(1, main_string[0]));
EDIT: Removed alternate solution after realising it duplicates #Tim's.
no need for strcpy for a single character.
string str ("x");
str[0] = main_string[0];
vector_string.push_back(str);

gcc linker errors when using boost to_lower & trim

I'm trying to use the boost library in my code but get the following linker errors under Sparc Solaris platform.
The problem code can essentially be summarised to:
#include <boost/algorithm/string.hpp>
std::string xparam;
...
xparam = boost::to_lower(xparam);
The linker error is:
LdapClient.cc:349: no match for `std::string& = void' operator
/opt/gcc-3.2.3/include/c++/3.2.3/bits/basic_string.h:338: candidates are: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/opt/gcc-3.2.3/include/c++/3.2.3/bits/basic_string.h:341: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/opt/gcc-3.2.3/include/c++/3.2.3/bits/basic_string.h:344: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
gmake: *** [LdapClient.o] Error 1
Any ideas?
boost::to_lower does not return a copy of the string, it operates on the variable passed into the function. For some examples, read this.
So no need to reassign:
boost::to_lower(xparam);
You will get an error because you are trying to assign the string to the value void.
If you want to make a copy of it, use the copy version:
std::string xparamLowered = boost::to_lower_copy(xparam);
boost::to_lower modifies the string in-place, it does not return a new string. This is sufficient:
boost::to_lower(xparam);
And your code doesn't compile because the return type of to_lower is void (as the error message says).