c++ libconfig ambiguous overload - c++

I'm about to compile a pretty easy 'Hello, world' using libconfig. But when I compile such code:
#include <iostream>
#include <libconfig.h++>
libconfig::Config cfg;
std::string target = "World";
int main(void)
{
try
{
cfg.readFile("greetings.cfg");
}
catch (const libconfig::FileIOException &fioex)
{
std::cerr << "I/O error while reading file." << std::endl;
return 1;
}
catch (const libconfig::ParseException &pex)
{
std::cerr << pex.getFile() << " " << pex.getLine()
<< ": " << pex.getError() << std::endl;
return 1;
}
try
{
target = cfg.lookup("target");
}
catch (const libconfig::SettingNotFoundException &nfex)
{
std::cerr << "No target set in configuration file. Using default." << std::endl;
}
std::cout << "Hello, " << target << "!" << std::endl;
return 0;
}
I have this error:
example1.cpp: In function 'int main()':
example1.cpp:28: error: ambiguous overload for 'operator=' in 'target = cfg.libconfig::Config::lookup(((const char*)"target"))
/usr/include/c++/4.2/bits/basic_string.h:490: note: 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>]
/usr/include/c++/4.2/bits/basic_string.h:498: 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>]
/usr/include/c++/4.2/bits/basic_string.h:509: 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>]

According to Chapter 4 of the documentation, on page 19, lookup returns a Setting&, not a string.
Now, according to page 20, Setting has a bunch of implicit conversions to various types, including std::string. Here, the conversion to std::string is ambiguous in the presence of the conversion to const char*, since std::string has constructors accepting both with equal rank.
This problem is actually explicitly described on page 21, wherein resolving the ambiguity with an explicit conversion (or "cast") is suggested, or the use of the member c_str() rather than of the conversion operators:
target = cfg.lookup("target").c_str();

Related

Why boost::replace_all_copy is not working? 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>(...)

String::copy error

I have the following code and get the error later described how do i correct this?
The objective behind is to parse the following sentence into variables :
temp1+temp2=10
Code:
int main()
{
string line,var1;
int limit,len;
//some code here
// parse function declarartion :string parse(string ,char ,int &)
f1>>line;
len=line.length();
var1=parse(line,'+',limit);
line.copy(line,len-limit,limit);
//some code here
}
Error:
alice.cpp: In function ‘int main()’:
alice.cpp:65:40: error: no matching function for call to ‘std::basic_string<char>::copy(std::string&, int, int&)’
alicebob.cpp:65:40: note: candidate is:
/usr/include/c++/4.6/bits/basic_string.tcc:724:5: note: std::basic_string<_CharT, _Traits, _Alloc>::size_type std::basic_string<_CharT, _Traits, _Alloc>::copy(_CharT*, std::basic_string<_CharT, _Traits, _Alloc>::size_type, std::basic_string<_CharT, _Traits, _Alloc>::size_type) const [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>, std::basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int]
/usr/include/c++/4.6/bits/basic_string.tcc:724:5: note: no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘char*’
If you just want to copy a substring into another string, why don't you use substr() instead?
int main()
{
std::string line("ceva5");
std::string var1;
int limit = 1,len;
//some code here
// parse function declarartion :string parse(string ,char ,int &)
// f1>>line;
len=line.length();
//
// var1=parse(line,'+',limit);
line = line.substr(limit, len-limit);
//some code here
std::cout << line << std::endl;
}
This should do what you want.
EDIT: I have not implemented your function but changed the code to work as string's copy().
Here is the explain: http://www.cplusplus.com/reference/string/string/copy/ ,The first arg is char*, So,Accroding to your question,I think your code should be
line.copy((char *)line.c_str(),len-limit,limit); // wrong code, lost the const
but this code is dangerous,Because std::copy()'s first arg is an array of characters to store the string 's substring, So,I think you code has something wrong.
Here is simple code:
std::string test1 = "test1";
char buffer[10] = {0};
test1.copy(buffer,2,3);
std::cout << "buffer is: " << buffer << std::endl;
and the output is :"buffer is: t1".

Why does istream_iterator<string>(ifstream("test.txt")) cause an error?

I have tried to write a code to read strings from file named "test.txt" and write the strings to standard output. The code below works well:
int main()
{
using namespace std;
ifstream file("test.txt");
copy(istream_iterator<string>(file),
istream_iterator<string>(),
ostream_iterator<string>(cout, " "));
}
However, with this modification, the code no longer compiles:
int main()
{
using namespace std;
copy(istream_iterator<string>(ifstream("test.txt")), // <-- Error here
istream_iterator<string>(),
ostream_iterator<string>(cout, " "));
}
Why doesn't this version compile?
The compiler I used is g++4.6.2, and the error as below:
ex11-16.cpp:16:65: error: no matching function for call to 'std::istream_iterator<std::basic_string<char> >::istream_iterator(std::ifstream)'
ex11-16.cpp:16:65: note: candidates are:
.../bits/stream_iterator.h:72:7: note: std::istream_iterator<_Tp, _CharT, _Traits, _Dist>::istream_iterator(const std::istream_iterator<_Tp, _CharT, _Traits, _Dist>&) [with _Tp = std::basic_string<char>, _CharT = char, _Traits = std::char_traits<char>, _Dist = int, std::istream_iterator<_Tp, _CharT, _Traits, _Dist> = std::istream_iterator<std::basic_string<char> >]
.../bits/stream_iterator.h:72:7: note: no known conversion for argument 1 from 'std::ifstream {aka std::basic_ifstream<char>}' to 'const std::istream_iterator<std::basic_string<char> >&'
.../bits/stream_iterator.h:68:7: note: std::istream_iterator<_Tp, _CharT, _Traits, _Dist>::istream_iterator(std::istream_iterator<_Tp, _CharT, _Traits, _Dist>::istream_type&) [with _Tp = std::basic_string<char>, _CharT = char, _Traits = std::char_traits<char>, _Dist = int, std::istream_iterator<_Tp, _CharT, _Traits, _Dist>::istream_type = std::basic_istream<char>]
.../bits/stream_iterator.h:68:7: note: no known conversion for argument 1 from 'std::ifstream {aka std::basic_ifstream<char>}' to 'std::istream_iterator<std::basic_string<char> >::istream_type& {aka std::basic_istream<char>&}'
.../bits/stream_iterator.h:64:26: note: std::istream_iterator<_Tp, _CharT, _Traits, _Dist>::istream_iterator() [with _Tp = std::basic_string<char>, _CharT = char, _Traits = std::char_traits<char>, _Dist = int]
.../bits/stream_iterator.h:64:26: note: candidate expects 0 arguments, 1 provided
There are (I believe) two errors here:
(1) You need to put quotations around test.txt:
istream_iterator<string>(ifstream("test.txt"), ... );
(2) istream_iterator's constructor takes in an istream& (that is, an lvalue reference to a stream). Consequently, the stream that you pass in has to be an lvalue. However, passing in ifstream("test.txt") passes in a temporary object of type ifstream, which is an rvalue rather than an lvalue. This is the same reason that you can't do this:
int function(int& x) {
x++;
}
int main() {
function(137); // Error - 137 is an rvalue, but lvalue is needed.
}
Hope this helps!

Mex files on Mountain Lion: explicit instantiation error

I'm working with MATLAB r2012b on Mountain Lion, and XCode version installed is 4.6.1. I've also used the patch at http://www.mathworks.it/support/solutions/en/data/1-IXBVKD/ and then I have typed "mex - setup" and selected the only option available
1: /Applications/MATLAB_R2012b.app/bin/mexopts.sh : Template Options file for building gcc MEX-files
I'm trying to use a framework for sparse modeling (written in C++) which uses mex files. In order to install the previous framework I have to call a matlab file inside the framework which calls mex function: however when I call this function I get the following message...
compilation of: -I./linalg/ -I./decomp/ -I./dictLearn/ dictLearn/mex/mexTrainDL.cpp
./linalg/linalg.h: In member function 'void Matrix<T>::print(const std::string&) const [with T = float]':
./linalg/linalg.h:1084: instantiated from 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]'
./linalg/linalg.h:1084: instantiated from 'void Matrix<T>::print(const std::string&) const [with T = float]'
dictLearn/mex/mexTrainDL.cpp:197: instantiated from here
./linalg/linalg.h:1084: error: explicit instantiation of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]' but no definition available
./linalg/linalg.h: At global scope:
./linalg/linalg.h: In instantiation of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]':
./linalg/linalg.h:1084: instantiated from 'void Matrix<T>::print(const std::string&) const [with T = float]'
dictLearn/mex/mexTrainDL.cpp:197: instantiated from here
./linalg/linalg.h:1084: error: explicit instantiation of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]' but no definition available
./linalg/linalg.h: In instantiation of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]':
./linalg/linalg.h:1084: instantiated from 'void Matrix<T>::print(const std::string&) const [with T = float]'
dictLearn/mex/mexTrainDL.cpp:197: instantiated from here
./linalg/linalg.h:1084: error: explicit instantiation of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]' but no definition available
mex: compile of ' "dictLearn/mex/mexTrainDL.cpp"' failed.
The problem is not connected to the framework. Googling I have seen that I'm not the only one with this problem with matlab, but I don't know how to fix it. Thank you very much in advance for any help.
Mattia
UPDATE:
I've discovered that the error is produced by the following blocks of code
Block 1:
/// print the sparse matrix
template<typename T> inline void SpMatrix<T>::print(const string& name) const {
cerr << name << endl;
cerr << _m << " x " << _n << " , " << _nzmax << endl;
for (int i = 0; i<_n; ++i) {
for (int j = _pB[i]; j<_pE[i]; ++j) {
cerr << "(" <<_r[j] << "," << i << ") = " << _v[j] << endl;
}
}
};
Block 2
/// Print the matrix to std::cout
template <typename T> inline void Matrix<T>::print(const string& name) const {
std::cerr << name << std::endl;
std::cerr << _m << " x " << _n << std::endl;
for (int i = 0; i<_m; ++i) {
for (int j = 0; j<_n; ++j) {
printf("%10.5g ",static_cast<double>(_X[j*_m+i]));
// std::cerr << _X[j*_m+i] << " ";
}
printf("\n ");
//std::cerr << std::endl;
}
printf("\n ");
};
Block 3
template <typename T> void ShiftMatrix<T>::print(const string& name) const {
cerr << name << endl;
cerr << "Shift Matrix: " << _shifts << " shifts" << endl;
_inputmatrix->print(name);
};
And block 4
template <typename T> void DoubleRowMatrix<T>::print(const string& name) const {
cerr << name << endl;
cerr << "Double Row Matrix" << endl;
_inputmatrix->print(name);
};
and the error is always due to the first line of the above blocks, that is ...
cerr << name << endl;
Commenting the above line in the 4 blocks the compiling terminates successfully.
Does somebody know why this happens?
I solved this problem by adding the compile flag "-mmacosx-version-min=10.7" via the line
compile_flags=[compile_flags ' -mmacosx-version-min=10.7'];
in the SPAMS framework's compile.m immediately before the for block that calls mex.
This solution came from Trying to build muParser: error: explicit instantiation of 'std::basic_ostream but no definition available and Building libsigc++ fails (std::basic_ostream explicit instantiation) which appear to be the same problem with other packages.
I solved a similar problem by changing the MACOSX_DEPLOYMENT_TARGET='10.5' to '10.6' in mexopts.sh.

C++ what is wrong with this function

void webparser(const string siteurl,string filename)
{
stringstream ss;
ss << "lynx -dump '" << siteurl << "'" > filename;
system(ss.str().c_str());
}
How do I use filename instead of file.txt in the above example?
Initially my line for ss was
ss << "lynx -dump '" << siteurl << "' > file.txt";
But I decided to change it to use the value from a parameter, so I added filename.
Below is my error message
main.cpp: In function ‘void webparser(std::string, std::string)’:
main.cpp:244:46: error: no match for ‘operator>’ in ‘std::operator<< [with _Traits = std::char_traits<char>]((* & std::operator<< [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]((* & std::operator<< [with _Traits = std::char_traits<char>]((* & ss.std::basic_stringstream<char>::<anonymous>.std::basic_iostream<char>::<anonymous>), ((const char*)"lynx -dump \'"))), (* & siteurl))), ((const char*)"\'")) > filename’
main.cpp:244:46: note: candidates are:
/usr/include/c++/4.6/bits/stl_pair.h:220:5: note: template<class _T1, class _T2> bool std::operator>(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
/usr/include/c++/4.6/bits/stl_iterator.h:303:5: note: template<class _Iterator> bool std::operator>(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
/usr/include/c++/4.6/bits/stl_iterator.h:353:5: note: template<class _IteratorL, class _IteratorR> bool std::operator>(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&)
/usr/include/c++/4.6/bits/basic_string.h:2547:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator>(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/basic_string.h:2559:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator>(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
/usr/include/c++/4.6/bits/basic_string.h:2571:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator>(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/stl_iterator.h:842:5: note: template<class _Iterator, class _Container> bool __gnu_cxx::operator>(const __gnu_cxx::__normal_iterator<_Iterator, _Container>&, const __gnu_cxx::__normal_iterator<_Iterator, _Container>&)
/usr/include/c++/4.6/bits/stl_iterator.h:836:5: note: template<class _IteratorL, class _IteratorR, class _Container> bool __gnu_cxx::operator>(const __gnu_cxx::__normal_iterator<_IteratorL, _Container>&, const __gnu_cxx::__normal_iterator<_IteratorR, _Container>&)
make: *** [main.o] Error 1
Replace
ss << "lynx -dump '" << siteurl << "'" > filename;
with
ss << "lynx -dump '" << siteurl << "'" << filename;
or, to be consistent with your original code:
ss << "lynx -dump '" << siteurl << "' > " << filename;