iterating through json property keys and values using boost property tree - c++

I am trying to read a json file using boost::property_tree. I have been able to read the json file and get values of properties. Now, I am trying to iterate through all the properties within the property tree and print out all the key value pairs. I'm not sure how to exactly do this. I have tried using BOOST_FOREACH but I end up getting a compile error. How do I properly iterate through the properties in the property tree variable and get their respective keys and values?
#include <fstream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/foreach.hpp>
#include <exception>
#include <string>
using namespace std;
int main(){
boost::property_tree::ptree pt;
boost::property_tree::read_json("./data.json", pt);
BOOST_FOREACH(boost::property_tree::ptree::value_type &v, pt)
{
std::cout << v.first << ":" << v.second << std::endl;
}
std::cout<<pt.get<string>("name")<<std::endl;
std::cout <<"Done"<<std::endl;
return 1;
}
I get a compiler error on the line std::cout << v.first << ":" << v.second << std::endl;. I am compiling using g++ -o test test.c. The compile error is:
test.c: In function 'int main()':
test.c:20:42: error: no match for 'operator<<' in 'std::operator<< <std::char_traits<char> >((* & std::operator<< <char, std::char_traits<char>, std::allocator<char> >((* & std::cout), (* & v.std::pair<const std::basic_string<char>, boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> > >::first))), ((const char*)":")) << v.std::pair<const std::basic_string<char>, boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> > >::second'
test.c:20:42: note: candidates are:
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/istream:41:0,
from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/fstream:40,
from test.c:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:106:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ostream_type& (*)(std::basic_ostream<_CharT, _Traits>::__ostream_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:106:7: note: no known conversion for argument 1 from 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >' to 'std::basic_ostream<char>::__ostream_type& (*)(std::basic_ostream<char>::__ostream_type&) {aka std::basic_ostream<char>& (*)(std::basic_ostream<char>&)}'
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:115:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ios_type& (*)(std::basic_ostream<_CharT, _Traits>::__ios_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>; std::basic_ostream<_CharT, _Traits>::__ios_type = std::basic_ios<char>]
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:115:7: note: no known conversion for argument 1 from 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >' to 'std::basic_ostream<char>::__ios_type& (*)(std::basic_ostream<char>::__ios_type&) {aka std::basic_ios<char>& (*)(std::basic_ios<char>&)}'
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:125:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:125:7: note: no known conversion for argument 1 from 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >' to 'std::ios_base& (*)(std::ios_base&)'
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:164:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:164:7: note: no known conversion for argument 1 from 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >' to 'long int'
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:168:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:168:7: note: no known conversion for argument 1 from 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >' to 'long unsigned int'
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:172:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:172:7: note: no known conversion for argument 1 from 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >' to 'bool'
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:607:0,
from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/istream:41,
from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/fstream:40,
from test.c:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/bits/ostream.tcc:93:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char; _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/bits/ostream.tcc:93:5: note: no known conversion for argument 1 from 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >' to 'short int'
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/istream:41:0,
from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/fstream:40,
from test.c:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:179:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:179:7: note: no known conversion for argument 1 from 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >' to 'short unsigned int'
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:607:0,
from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/istream:41,
from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/fstream:40,
from test.c:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/bits/ostream.tcc:107:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char; _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/bits/ostream.tcc:107:5: note: no known conversion for argument 1 from 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >' to 'int'
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/istream:41:0,
from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/fstream:40,
from test.c:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:190:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:190:7: note: no known conversion for argument 1 from 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >' to 'unsigned int'
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:199:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:199:7: note: no known conversion for argument 1 from 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >' to 'long long int'
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:203:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:203:7: note: no known conversion for argument 1 from 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >' to 'long long unsigned int'
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:218:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:218:7: note: no known conversion for argument 1 from 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >' to 'double'
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:222:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:222:7: note: no known conversion for argument 1 from 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >' to 'float'
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:230:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:230:7: note: no known conversion for argument 1 from 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >' to 'long double'
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:243:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:243:7: note: no known conversion for argument 1 from 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >' to 'const void*'
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:607:0,
from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/istream:41,
from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/fstream:40,
from test.c:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/bits/ostream.tcc:121:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__streambuf_type*) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/bits/ostream.tcc:121:5: note: no known conversion for argument 1 from 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >' to 'std::basic_ostream<char>::__streambuf_type* {aka std::basic_streambuf<char>*}'
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/string:54:0,
from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/bits/locale_classes.h:42,
from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/bits/ios_base.h:43,
from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ios:43,
from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/istream:40,
from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/fstream:40,
from test.c:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/bits/basic_string.h:2750:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/bits/basic_string.h:2750:5: note: template argument deduction/substitution failed:
test.c:20:42: note: 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >' is not derived from 'const std::basic_string<_CharT, _Traits, _Alloc>'
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/istream:41:0,
from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/fstream:40,
from test.c:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:469:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, _CharT)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:469:5: note: template argument deduction/substitution failed:
test.c:20:42: note: deduced conflicting types for parameter '_CharT' ('char' and 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >')
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/istream:41:0,
from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/fstream:40,
from test.c:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:474:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, char)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:474:5: note: template argument deduction/substitution failed:
test.c:20:42: note: cannot convert 'v.std::pair<const std::basic_string<char>, boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> > >::second' (type 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >') to type 'char'
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/istream:41:0,
from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/fstream:40,
from test.c:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:480:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, char)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:480:5: note: template argument deduction/substitution failed:
test.c:20:42: note: cannot convert 'v.std::pair<const std::basic_string<char>, boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> > >::second' (type 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >') to type 'char'
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/istream:41:0,
from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/fstream:40,
from test.c:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:486:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, signed char)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:486:5: note: template argument deduction/substitution failed:
test.c:20:42: note: cannot convert 'v.std::pair<const std::basic_string<char>, boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> > >::second' (type 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >') to type 'signed char'
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/istream:41:0,
from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/fstream:40,
from test.c:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:491:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, unsigned char)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:491:5: note: template argument deduction/substitution failed:
test.c:20:42: note: cannot convert 'v.std::pair<const std::basic_string<char>, boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> > >::second' (type 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >') to type 'unsigned char'
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/istream:41:0,
from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/fstream:40,
from test.c:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:511:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const _CharT*)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:511:5: note: template argument deduction/substitution failed:
test.c:20:42: note: mismatched types 'const _CharT*' and 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >'
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:607:0,
from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/istream:41,
from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/fstream:40,
from test.c:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/bits/ostream.tcc:323:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const char*)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/bits/ostream.tcc:323:5: note: template argument deduction/substitution failed:
test.c:20:42: note: cannot convert 'v.std::pair<const std::basic_string<char>, boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> > >::second' (type 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >') to type 'const char*'
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/istream:41:0,
from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/fstream:40,
from test.c:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:528:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const char*)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:528:5: note: template argument deduction/substitution failed:
test.c:20:42: note: cannot convert 'v.std::pair<const std::basic_string<char>, boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> > >::second' (type 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >') to type 'const char*'
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/istream:41:0,
from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/fstream:40,
from test.c:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:541:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const signed char*)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:541:5: note: template argument deduction/substitution failed:
test.c:20:42: note: cannot convert 'v.std::pair<const std::basic_string<char>, boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> > >::second' (type 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >') to type 'const signed char*'
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/istream:41:0,
from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/fstream:40,
from test.c:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:546:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const unsigned char*)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:546:5: note: template argument deduction/substitution failed:
test.c:20:42: note: cannot convert 'v.std::pair<const std::basic_string<char>, boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> > >::second' (type 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >') to type 'const unsigned char*'
In file included from /usr/include/boost/spirit/home/classic/utility/impl/chset/basic_chset.hpp:13:0,
from /usr/include/boost/spirit/home/classic/utility/chset.hpp:16,
from /usr/include/boost/spirit/home/classic/utility.hpp:26,
from /usr/include/boost/spirit/home/classic.hpp:29,
from /usr/include/boost/spirit/include/classic.hpp:11,
from /usr/include/boost/property_tree/detail/json_parser_read.hpp:18,
from /usr/include/boost/property_tree/json_parser.hpp:14,
from test.c:3:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/bitset:1523:5: note: template<class _CharT, class _Traits, long unsigned int _Nb> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::bitset<_Nb>&)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/bitset:1523:5: note: template argument deduction/substitution failed:
test.c:20:42: note: 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >' is not derived from 'const std::bitset<_Nb>'
In file included from /usr/include/boost/property_tree/detail/json_parser_write.hpp:18:0,
from /usr/include/boost/property_tree/json_parser.hpp:15,
from test.c:3:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/iomanip:78:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, std::_Resetiosflags)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/iomanip:78:5: note: template argument deduction/substitution failed:
test.c:20:42: note: cannot convert 'v.std::pair<const std::basic_string<char>, boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> > >::second' (type 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >') to type 'std::_Resetiosflags'
In file included from /usr/include/boost/property_tree/detail/json_parser_write.hpp:18:0,
from /usr/include/boost/property_tree/json_parser.hpp:15,
from test.c:3:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/iomanip:108:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, std::_Setiosflags)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/iomanip:108:5: note: template argument deduction/substitution failed:
test.c:20:42: note: cannot convert 'v.std::pair<const std::basic_string<char>, boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> > >::second' (type 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >') to type 'std::_Setiosflags'
In file included from /usr/include/boost/property_tree/detail/json_parser_write.hpp:18:0,
from /usr/include/boost/property_tree/json_parser.hpp:15,
from test.c:3:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/iomanip:142:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, std::_Setbase)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/iomanip:142:5: note: template argument deduction/substitution failed:
test.c:20:42: note: cannot convert 'v.std::pair<const std::basic_string<char>, boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> > >::second' (type 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >') to type 'std::_Setbase'
In file included from /usr/include/boost/property_tree/detail/json_parser_write.hpp:18:0,
from /usr/include/boost/property_tree/json_parser.hpp:15,
from test.c:3:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/iomanip:177:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, std::_Setfill<_CharT>)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/iomanip:177:5: note: template argument deduction/substitution failed:
test.c:20:42: note: 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >' is not derived from 'std::_Setfill<_CharT>'
In file included from /usr/include/boost/property_tree/detail/json_parser_write.hpp:18:0,
from /usr/include/boost/property_tree/json_parser.hpp:15,
from test.c:3:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/iomanip:207:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, std::_Setprecision)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/iomanip:207:5: note: template argument deduction/substitution failed:
test.c:20:42: note: cannot convert 'v.std::pair<const std::basic_string<char>, boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> > >::second' (type 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >') to type 'std::_Setprecision'
In file included from /usr/include/boost/property_tree/detail/json_parser_write.hpp:18:0,
from /usr/include/boost/property_tree/json_parser.hpp:15,
from test.c:3:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/iomanip:237:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, std::_Setw)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/iomanip:237:5: note: template argument deduction/substitution failed:
test.c:20:42: note: cannot convert 'v.std::pair<const std::basic_string<char>, boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> > >::second' (type 'boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >') to type 'std::_Setw'

I named my file test.c instead of test.cpp, so that could have been an issue. But changing that did not fix the problem. I changed v.second to v.second.data() and it worked fine.

Related

trying to use vector<vector<int>> in a class

starting to learn classes in c++ and i came across a simple board game... but i got this insane amount of error that i cant solve.
if i shouldn't post this kind of question here, please advise me.
Board.h:
#include<vector>
using namespace std;
class Board{
int largura;
int altura;
public:
Board(int x,int y) : largura(x), altura(y) {}
vector<vector<int> > mat();
void printa();
};
Board.cpp:
#include "Board.h"
#include <vector>
#include <iostream>
using namespace std;
vector<vector<int> > Board::mat() {
vector<vector<int> > v(altura, vector<int>(largura,1));
return v;
}
void Board::printa() {
auto matriz = mat();
vector<int> vec(largura,0);
for(;;) {
for (auto i:vec) {
for (auto j:vec) {
cout << matriz[i,j];
}
cout << "\n";
}
}
}
main.cpp:
#include<iostream>
#include "Board.h"
#include <vector>
using namespace std;
int main(){
Board tela(5, 5);
tela.mat();
tela.printa();
return 0;
}
the error:
[Rafael#localhost untitled]$ g++ -std=c++17 main.cpp Board.cpp -o board
Board.cpp: In member function ‘void Board::printa()’:
Board.cpp:22:22: error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> > >::value_type {aka std::vector<int>}’)
cout << matriz[i,j];
In file included from /usr/include/c++/6.3.1/iostream:39:0,
from Board.cpp:7:
/usr/include/c++/6.3.1/ostream:108:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ostream_type& (*)(std::basic_ostream<_CharT, _Traits>::__ostream_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(__ostream_type& (*__pf)(__ostream_type&))
^~~~~~~~
/usr/include/c++/6.3.1/ostream:108:7: note: no known conversion for argument 1 from ‘__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> > >::value_type {aka std::vector<int>}’ to ‘std::basic_ostream<char>::__ostream_type& (*)(std::basic_ostream<char>::__ostream_type&) {aka std::basic_ostream<char>& (*)(std::basic_ostream<char>&)}’
/usr/include/c++/6.3.1/ostream:117:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ios_type& (*)(std::basic_ostream<_CharT, _Traits>::__ios_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>; std::basic_ostream<_CharT, _Traits>::__ios_type = std::basic_ios<char>]
operator<<(__ios_type& (*__pf)(__ios_type&))
^~~~~~~~
/usr/include/c++/6.3.1/ostream:117:7: note: no known conversion for argument 1 from ‘__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> > >::value_type {aka std::vector<int>}’ to ‘std::basic_ostream<char>::__ios_type& (*)(std::basic_ostream<char>::__ios_type&) {aka std::basic_ios<char>& (*)(std::basic_ios<char>&)}’
/usr/include/c++/6.3.1/ostream:127:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(ios_base& (*__pf) (ios_base&))
^~~~~~~~
/usr/include/c++/6.3.1/ostream:127:7: note: no known conversion for argument 1 from ‘__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> > >::value_type {aka std::vector<int>}’ to ‘std::ios_base& (*)(std::ios_base&)’
/usr/include/c++/6.3.1/ostream:166:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(long __n)
^~~~~~~~
/usr/include/c++/6.3.1/ostream:166:7: note: no known conversion for argument 1 from ‘__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> > >::value_type {aka std::vector<int>}’ to ‘long int’
/usr/include/c++/6.3.1/ostream:170:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(unsigned long __n)
^~~~~~~~
/usr/include/c++/6.3.1/ostream:170:7: note: no known conversion for argument 1 from ‘__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> > >::value_type {aka std::vector<int>}’ to ‘long unsigned int’
/usr/include/c++/6.3.1/ostream:174:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(bool __n)
^~~~~~~~
/usr/include/c++/6.3.1/ostream:174:7: note: no known conversion for argument 1 from ‘__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> > >::value_type {aka std::vector<int>}’ to ‘bool’
In file included from /usr/include/c++/6.3.1/ostream:638:0,
from /usr/include/c++/6.3.1/iostream:39,
from Board.cpp:7:
/usr/include/c++/6.3.1/bits/ostream.tcc:91:5: note: candidate: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char; _Traits = std::char_traits<char>]
basic_ostream<_CharT, _Traits>::
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/6.3.1/bits/ostream.tcc:91:5: note: no known conversion for argument 1 from ‘__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> > >::value_type {aka std::vector<int>}’ to ‘short int’
In file included from /usr/include/c++/6.3.1/iostream:39:0,
from Board.cpp:7:
/usr/include/c++/6.3.1/ostream:181:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(unsigned short __n)
^~~~~~~~
/usr/include/c++/6.3.1/ostream:181:7: note: no known conversion for argument 1 from ‘__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> > >::value_type {aka std::vector<int>}’ to ‘short unsigned int’
In file included from /usr/include/c++/6.3.1/ostream:638:0,
from /usr/include/c++/6.3.1/iostream:39,
from Board.cpp:7:
/usr/include/c++/6.3.1/bits/ostream.tcc:105:5: note: candidate: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char; _Traits = std::char_traits<char>]
basic_ostream<_CharT, _Traits>::
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/6.3.1/bits/ostream.tcc:105:5: note: no known conversion for argument 1 from ‘__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> > >::value_type {aka std::vector<int>}’ to ‘int’
In file included from /usr/include/c++/6.3.1/iostream:39:0,
from Board.cpp:7:
/usr/include/c++/6.3.1/ostream:192:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(unsigned int __n)
^~~~~~~~
/usr/include/c++/6.3.1/ostream:192:7: note: no known conversion for argument 1 from ‘__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> > >::value_type {aka std::vector<int>}’ to ‘unsigned int’
/usr/include/c++/6.3.1/ostream:201:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(long long __n)
^~~~~~~~
/usr/include/c++/6.3.1/ostream:201:7: note: no known conversion for argument 1 from ‘__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> > >::value_type {aka std::vector<int>}’ to ‘long long int’
/usr/include/c++/6.3.1/ostream:205:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(unsigned long long __n)
^~~~~~~~
/usr/include/c++/6.3.1/ostream:205:7: note: no known conversion for argument 1 from ‘__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> > >::value_type {aka std::vector<int>}’ to ‘long long unsigned int’
/usr/include/c++/6.3.1/ostream:220:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(double __f)
^~~~~~~~
/usr/include/c++/6.3.1/ostream:220:7: note: no known conversion for argument 1 from ‘__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> > >::value_type {aka std::vector<int>}’ to ‘double’
/usr/include/c++/6.3.1/ostream:224:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(float __f)
^~~~~~~~
/usr/include/c++/6.3.1/ostream:224:7: note: no known conversion for argument 1 from ‘__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> > >::value_type {aka std::vector<int>}’ to ‘float’
/usr/include/c++/6.3.1/ostream:232:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(long double __f)
^~~~~~~~
/usr/include/c++/6.3.1/ostream:232:7: note: no known conversion for argument 1 from ‘__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> > >::value_type {aka std::vector<int>}’ to ‘long double’
/usr/include/c++/6.3.1/ostream:245:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(const void* __p)
^~~~~~~~
/usr/include/c++/6.3.1/ostream:245:7: note: no known conversion for argument 1 from ‘__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> > >::value_type {aka std::vector<int>}’ to ‘const void*’
In file included from /usr/include/c++/6.3.1/ostream:638:0,
from /usr/include/c++/6.3.1/iostream:39,
from Board.cpp:7:
/usr/include/c++/6.3.1/bits/ostream.tcc:119:5: note: candidate: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__streambuf_type*) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]
basic_ostream<_CharT, _Traits>::
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/6.3.1/bits/ostream.tcc:119:5: note: no known conversion for argument 1 from ‘__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> > >::value_type {aka std::vector<int>}’ to ‘std::basic_ostream<char>::__streambuf_type* {aka std::basic_streambuf<char>*}’
In file included from /usr/include/c++/6.3.1/string:52:0,
from /usr/include/c++/6.3.1/bits/locale_classes.h:40,
from /usr/include/c++/6.3.1/bits/ios_base.h:41,
from /usr/include/c++/6.3.1/ios:42,
from /usr/include/c++/6.3.1/ostream:38,
from /usr/include/c++/6.3.1/iostream:39,
from Board.cpp:7:
/usr/include/c++/6.3.1/bits/basic_string.h:5325:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)
operator<<(basic_ostream<_CharT, _Traits>& __os,
^~~~~~~~
/usr/include/c++/6.3.1/bits/basic_string.h:5325:5: note: template argument deduction/substitution failed:
Board.cpp:22:35: note: ‘__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> > >::value_type {aka std::vector<int>}’ is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
cout << matriz[i,j];
^
In file included from /usr/include/c++/6.3.1/bits/ios_base.h:46:0,
from /usr/include/c++/6.3.1/ios:42,
from /usr/include/c++/6.3.1/ostream:38,
from /usr/include/c++/6.3.1/iostream:39,
from Board.cpp:7:
/usr/include/c++/6.3.1/system_error:209:5: note: candidate: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::error_code&)
operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __e)
^~~~~~~~
/usr/include/c++/6.3.1/system_error:209:5: note: template argument deduction/substitution failed:
Board.cpp:22:35: note: cannot convert ‘matriz.std::vector<_Tp, _Alloc>::operator[]<std::vector<int>, std::allocator<std::vector<int> > >(((void)0, ((std::vector<std::vector<int> >::size_type)j)))’ (type ‘__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> > >::value_type {aka std::vector<int>}’) to type ‘const std::error_code&’
cout << matriz[i,j];
^
In file included from /usr/include/c++/6.3.1/iostream:39:0,
from Board.cpp:7:
/usr/include/c++/6.3.1/ostream:497:5: note: candidate: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, _CharT)
operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
^~~~~~~~
/usr/include/c++/6.3.1/ostream:497:5: note: template argument deduction/substitution failed:
Board.cpp:22:35: note: deduced conflicting types for parameter ‘_CharT’ (‘char’ and ‘std::vector<int>’)
cout << matriz[i,j];
^
In file included from /usr/include/c++/6.3.1/iostream:39:0,
from Board.cpp:7:
/usr/include/c++/6.3.1/ostream:502:5: note: candidate: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, char)
operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
^~~~~~~~
/usr/include/c++/6.3.1/ostream:502:5: note: template argument deduction/substitution failed:
Board.cpp:22:35: note: cannot convert ‘matriz.std::vector<_Tp, _Alloc>::operator[]<std::vector<int>, std::allocator<std::vector<int> > >(((void)0, ((std::vector<std::vector<int> >::size_type)j)))’ (type ‘__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> > >::value_type {aka std::vector<int>}’) to type ‘char’
cout << matriz[i,j];
^
In file included from /usr/include/c++/6.3.1/iostream:39:0,
from Board.cpp:7:
/usr/include/c++/6.3.1/ostream:508:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, char)
operator<<(basic_ostream<char, _Traits>& __out, char __c)
^~~~~~~~
/usr/include/c++/6.3.1/ostream:508:5: note: template argument deduction/substitution failed:
Board.cpp:22:35: note: cannot convert ‘matriz.std::vector<_Tp, _Alloc>::operator[]<std::vector<int>, std::allocator<std::vector<int> > >(((void)0, ((std::vector<std::vector<int> >::size_type)j)))’ (type ‘__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> > >::value_type {aka std::vector<int>}’) to type ‘char’
cout << matriz[i,j];
^
In file included from /usr/include/c++/6.3.1/iostream:39:0,
from Board.cpp:7:
/usr/include/c++/6.3.1/ostream:514:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, signed char)
operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
^~~~~~~~
/usr/include/c++/6.3.1/ostream:514:5: note: template argument deduction/substitution failed:
Board.cpp:22:35: note: cannot convert ‘matriz.std::vector<_Tp, _Alloc>::operator[]<std::vector<int>, std::allocator<std::vector<int> > >(((void)0, ((std::vector<std::vector<int> >::size_type)j)))’ (type ‘__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> > >::value_type {aka std::vector<int>}’) to type ‘signed char’
cout << matriz[i,j];
^
In file included from /usr/include/c++/6.3.1/iostream:39:0,
from Board.cpp:7:
/usr/include/c++/6.3.1/ostream:519:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, unsigned char)
operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
^~~~~~~~
/usr/include/c++/6.3.1/ostream:519:5: note: template argument deduction/substitution failed:
Board.cpp:22:35: note: cannot convert ‘matriz.std::vector<_Tp, _Alloc>::operator[]<std::vector<int>, std::allocator<std::vector<int> > >(((void)0, ((std::vector<std::vector<int> >::size_type)j)))’ (type ‘__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> > >::value_type {aka std::vector<int>}’) to type ‘unsigned char’
cout << matriz[i,j];
^
In file included from /usr/include/c++/6.3.1/iostream:39:0,
from Board.cpp:7:
/usr/include/c++/6.3.1/ostream:539:5: note: candidate: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const _CharT*)
operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
^~~~~~~~
/usr/include/c++/6.3.1/ostream:539:5: note: template argument deduction/substitution failed:
Board.cpp:22:35: note: mismatched types ‘const _CharT*’ and ‘std::vector<int>’
cout << matriz[i,j];
^
In file included from /usr/include/c++/6.3.1/ostream:638:0,
from /usr/include/c++/6.3.1/iostream:39,
from Board.cpp:7:
/usr/include/c++/6.3.1/bits/ostream.tcc:321:5: note: candidate: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const char*)
operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
^~~~~~~~
/usr/include/c++/6.3.1/bits/ostream.tcc:321:5: note: template argument deduction/substitution failed:
Board.cpp:22:35: note: cannot convert ‘matriz.std::vector<_Tp, _Alloc>::operator[]<std::vector<int>, std::allocator<std::vector<int> > >(((void)0, ((std::vector<std::vector<int> >::size_type)j)))’ (type ‘__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> > >::value_type {aka std::vector<int>}’) to type ‘const char*’
cout << matriz[i,j];
^
In file included from /usr/include/c++/6.3.1/iostream:39:0,
from Board.cpp:7:
/usr/include/c++/6.3.1/ostream:556:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const char*)
operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
^~~~~~~~
/usr/include/c++/6.3.1/ostream:556:5: note: template argument deduction/substitution failed:
Board.cpp:22:35: note: cannot convert ‘matriz.std::vector<_Tp, _Alloc>::operator[]<std::vector<int>, std::allocator<std::vector<int> > >(((void)0, ((std::vector<std::vector<int> >::size_type)j)))’ (type ‘__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> > >::value_type {aka std::vector<int>}’) to type ‘const char*’
cout << matriz[i,j];
^
In file included from /usr/include/c++/6.3.1/iostream:39:0,
from Board.cpp:7:
/usr/include/c++/6.3.1/ostream:569:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const signed char*)
operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
^~~~~~~~
/usr/include/c++/6.3.1/ostream:569:5: note: template argument deduction/substitution failed:
Board.cpp:22:35: note: cannot convert ‘matriz.std::vector<_Tp, _Alloc>::operator[]<std::vector<int>, std::allocator<std::vector<int> > >(((void)0, ((std::vector<std::vector<int> >::size_type)j)))’ (type ‘__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> > >::value_type {aka std::vector<int>}’) to type ‘const signed char*’
cout << matriz[i,j];
^
In file included from /usr/include/c++/6.3.1/iostream:39:0,
from Board.cpp:7:
/usr/include/c++/6.3.1/ostream:574:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const unsigned char*)
operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
^~~~~~~~
/usr/include/c++/6.3.1/ostream:574:5: note: template argument deduction/substitution failed:
Board.cpp:22:35: note: cannot convert ‘matriz.std::vector<_Tp, _Alloc>::operator[]<std::vector<int>, std::allocator<std::vector<int> > >(((void)0, ((std::vector<std::vector<int> >::size_type)j)))’ (type ‘__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> > >::value_type {aka std::vector<int>}’) to type ‘const unsigned char*’
cout << matriz[i,j];
^
In file included from /usr/include/c++/6.3.1/iostream:39:0,
from Board.cpp:7:
/usr/include/c++/6.3.1/ostream:628:5: note: candidate: std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = std::vector<int>] <near match>
operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)
^~~~~~~~
/usr/include/c++/6.3.1/ostream:628:5: note: conversion of argument 1 would be ill-formed:
Board.cpp:22:35: error: cannot bind ‘std::ostream {aka std::basic_ostream<char>}’ lvalue to ‘std::basic_ostream<char>&&’
cout << matriz[i,j];
[i,j] is a trap. It's the same thing as [j]. What you want is matriz[i][j];, not matriz[i,j].
(You should also avoid using directives in header files, but that's purely cosmetic. Your problem is the [i,j] part.)

overload ostream::<< to print vector of vectors

Working off Chris's answer in this post How to print out the contents of a vector? I am trying to overload ostream << to print a vector vectors. Here's what I'm reading in from a file, where each line becomes a vector:
1, 2, 3
4, 4, 4, 4, 4
7, 7
41, 52, 632
So the structure looks something like
v[0] = [1, 2, 3]
and so on.
I thought Chris's solution should apply perfectly to this situation, and it certainly works for a 1d vector, but I'm trying to understand why it doesn't work for a 2d vector. The code's below:
template <typename T>
std::ostream& operator << (std::ostream& out, const std::vector<T>& v){
if(! v.empty()){
std::cout<< '[';
std::copy(v.begin(), v.end(), std::ostream_iterator<T>(std::cout, " "));
std::cout<< ']' << std::endl;
}
else{
std::cout << "[ ]" << std::endl;
}
}
I'm thinking I need to replace the function in std::ostream_iterator to be << since that is what I am overriding in the first place, but I have tried << and ostream::<< and std:: << and these all yield compile-time errors. What would be the proper way to do this?
If I try to run the code above as is on a vector of vectors of ints, I have the following compile-time error:
In file included from /usr/include/c++/4.9/iterator:66:0,
from ctest.cpp:6:
/usr/include/c++/4.9/bits/stream_iterator.h: In instantiation of ‘std::ostream_iterator<_Tp, _CharT, _Traits>& std::ostream_iterator<_Tp, _CharT, _Traits>::operator=(const _Tp&) [with _Tp = std::vector<int>; _CharT = char; _Traits = std::char_traits<char>]’:
/usr/include/c++/4.9/bits/stl_algobase.h:336:18: required from ‘static _OI std::__copy_move<false, false, std::random_access_iterator_tag>::__copy_m(_II, _II, _OI) [with _II = const std::vector<int>*; _OI = std::ostream_iterator<std::vector<int>, char, std::char_traits<char> >]’
/usr/include/c++/4.9/bits/stl_algobase.h:396:70: required from ‘_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = const std::vector<int>*; _OI = std::ostream_iterator<std::vector<int>, char, std::char_traits<char> >]’
/usr/include/c++/4.9/bits/stl_algobase.h:434:38: required from ‘_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false; _II = __gnu_cxx::__normal_iterator<const std::vector<int>*, std::vector<std::vector<int> > >; _OI = std::ostream_iterator<std::vector<int>, char, std::char_traits<char> >]’
/usr/include/c++/4.9/bits/stl_algobase.h:466:17: required from ‘_OI std::copy(_II, _II, _OI) [with _II = __gnu_cxx::__normal_iterator<const std::vector<int>*, std::vector<std::vector<int> > >; _OI = std::ostream_iterator<std::vector<int>, char, std::char_traits<char> >]’
ctest.cpp:15:75: required from ‘std::ostream& operator<<(std::ostream&, const std::vector<T>&) [with T = std::vector<int>; std::ostream = std::basic_ostream<char>]’
ctest.cpp:70:18: required from here
/usr/include/c++/4.9/bits/stream_iterator.h:198:13: error: no match for ‘operator<<’ (operand types are ‘std::ostream_iterator<std::vector<int>, char, std::char_traits<char> >::ostream_type {aka std::basic_ostream<char>}’ and ‘const std::vector<int>’)
*_M_stream << __value;
^
/usr/include/c++/4.9/bits/stream_iterator.h:198:13: note: candidates are:
In file included from /usr/include/c++/4.9/iostream:39:0,
from ctest.cpp:1:
/usr/include/c++/4.9/ostream:108:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ostream_type& (*)(std::basic_ostream<_CharT, _Traits>::__ostream_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(__ostream_type& (*__pf)(__ostream_type&))
^
/usr/include/c++/4.9/ostream:108:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘std::basic_ostream<char>::__ostream_type& (*)(std::basic_ostream<char>::__ostream_type&) {aka std::basic_ostream<char>& (*)(std::basic_ostream<char>&)}’
/usr/include/c++/4.9/ostream:117:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ios_type& (*)(std::basic_ostream<_CharT, _Traits>::__ios_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>; std::basic_ostream<_CharT, _Traits>::__ios_type = std::basic_ios<char>]
operator<<(__ios_type& (*__pf)(__ios_type&))
^
/usr/include/c++/4.9/ostream:117:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘std::basic_ostream<char>::__ios_type& (*)(std::basic_ostream<char>::__ios_type&) {aka std::basic_ios<char>& (*)(std::basic_ios<char>&)}’
/usr/include/c++/4.9/ostream:127:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(ios_base& (*__pf) (ios_base&))
^
/usr/include/c++/4.9/ostream:127:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘std::ios_base& (*)(std::ios_base&)’
/usr/include/c++/4.9/ostream:166:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(long __n)
^
/usr/include/c++/4.9/ostream:166:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘long int’
/usr/include/c++/4.9/ostream:170:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(unsigned long __n)
^
/usr/include/c++/4.9/ostream:170:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘long unsigned int’
/usr/include/c++/4.9/ostream:174:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(bool __n)
^
/usr/include/c++/4.9/ostream:174:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘bool’
In file included from /usr/include/c++/4.9/ostream:612:0,
from /usr/include/c++/4.9/iostream:39,
from ctest.cpp:1:
/usr/include/c++/4.9/bits/ostream.tcc:91:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char; _Traits = std::char_traits<char>]
basic_ostream<_CharT, _Traits>::
^
/usr/include/c++/4.9/bits/ostream.tcc:91:5: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘short int’
In file included from /usr/include/c++/4.9/iostream:39:0,
from ctest.cpp:1:
/usr/include/c++/4.9/ostream:181:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(unsigned short __n)
^
/usr/include/c++/4.9/ostream:181:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘short unsigned int’
In file included from /usr/include/c++/4.9/ostream:612:0,
from /usr/include/c++/4.9/iostream:39,
from ctest.cpp:1:
/usr/include/c++/4.9/bits/ostream.tcc:105:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char; _Traits = std::char_traits<char>]
basic_ostream<_CharT, _Traits>::
^
/usr/include/c++/4.9/bits/ostream.tcc:105:5: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘int’
In file included from /usr/include/c++/4.9/iostream:39:0,
from ctest.cpp:1:
/usr/include/c++/4.9/ostream:192:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(unsigned int __n)
^
/usr/include/c++/4.9/ostream:192:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘unsigned int’
/usr/include/c++/4.9/ostream:201:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(long long __n)
^
/usr/include/c++/4.9/ostream:201:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘long long int’
/usr/include/c++/4.9/ostream:205:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(unsigned long long __n)
^
/usr/include/c++/4.9/ostream:205:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘long long unsigned int’
/usr/include/c++/4.9/ostream:220:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(double __f)
^
/usr/include/c++/4.9/ostream:220:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘double’
/usr/include/c++/4.9/ostream:224:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(float __f)
^
/usr/include/c++/4.9/ostream:224:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘float’
/usr/include/c++/4.9/ostream:232:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(long double __f)
^
/usr/include/c++/4.9/ostream:232:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘long double’
/usr/include/c++/4.9/ostream:245:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(const void* __p)
^
/usr/include/c++/4.9/ostream:245:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘const void*’
In file included from /usr/include/c++/4.9/ostream:612:0,
from /usr/include/c++/4.9/iostream:39,
from ctest.cpp:1:
/usr/include/c++/4.9/bits/ostream.tcc:119:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__streambuf_type*) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]
basic_ostream<_CharT, _Traits>::
^
/usr/include/c++/4.9/bits/ostream.tcc:119:5: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘std::basic_ostream<char>::__streambuf_type* {aka std::basic_streambuf<char>*}’
In file included from /usr/include/c++/4.9/string:52:0,
from /usr/include/c++/4.9/bits/locale_classes.h:40,
from /usr/include/c++/4.9/bits/ios_base.h:41,
from /usr/include/c++/4.9/ios:42,
from /usr/include/c++/4.9/ostream:38,
from /usr/include/c++/4.9/iostream:39,
from ctest.cpp:1:
/usr/include/c++/4.9/bits/basic_string.h:2772:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
/usr/include/c++/4.9/bits/basic_string.h:2772:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/4.9/iterator:66:0,
from ctest.cpp:6:
/usr/include/c++/4.9/bits/stream_iterator.h:198:13: note: ‘const std::vector<int>’ is not derived from ‘const std::basic_string<_CharT, _Traits, _Alloc>’
*_M_stream << __value;
^
In file included from /usr/include/c++/4.9/iostream:39:0,
from ctest.cpp:1:
/usr/include/c++/4.9/ostream:471:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, _CharT)
operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
^
/usr/include/c++/4.9/ostream:471:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/4.9/iterator:66:0,
from ctest.cpp:6:
/usr/include/c++/4.9/bits/stream_iterator.h:198:13: note: deduced conflicting types for parameter ‘_CharT’ (‘char’ and ‘std::vector<int>’)
*_M_stream << __value;
^
In file included from /usr/include/c++/4.9/iostream:39:0,
from ctest.cpp:1:
/usr/include/c++/4.9/ostream:476:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, char)
operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
^
/usr/include/c++/4.9/ostream:476:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/4.9/iterator:66:0,
from ctest.cpp:6:
/usr/include/c++/4.9/bits/stream_iterator.h:198:13: note: cannot convert ‘__value’ (type ‘const std::vector<int>’) to type ‘char’
*_M_stream << __value;
^
In file included from /usr/include/c++/4.9/iostream:39:0,
from ctest.cpp:1:
/usr/include/c++/4.9/ostream:482:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, char)
operator<<(basic_ostream<char, _Traits>& __out, char __c)
^
/usr/include/c++/4.9/ostream:482:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/4.9/iterator:66:0,
from ctest.cpp:6:
/usr/include/c++/4.9/bits/stream_iterator.h:198:13: note: cannot convert ‘__value’ (type ‘const std::vector<int>’) to type ‘char’
*_M_stream << __value;
^
In file included from /usr/include/c++/4.9/iostream:39:0,
from ctest.cpp:1:
/usr/include/c++/4.9/ostream:488:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, signed char)
operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
^
/usr/include/c++/4.9/ostream:488:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/4.9/iterator:66:0,
from ctest.cpp:6:
/usr/include/c++/4.9/bits/stream_iterator.h:198:13: note: cannot convert ‘__value’ (type ‘const std::vector<int>’) to type ‘signed char’
*_M_stream << __value;
^
In file included from /usr/include/c++/4.9/iostream:39:0,
from ctest.cpp:1:
/usr/include/c++/4.9/ostream:493:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, unsigned char)
operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
^
/usr/include/c++/4.9/ostream:493:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/4.9/iterator:66:0,
from ctest.cpp:6:
/usr/include/c++/4.9/bits/stream_iterator.h:198:13: note: cannot convert ‘__value’ (type ‘const std::vector<int>’) to type ‘unsigned char’
*_M_stream << __value;
^
In file included from /usr/include/c++/4.9/iostream:39:0,
from ctest.cpp:1:
/usr/include/c++/4.9/ostream:513:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const _CharT*)
operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
^
/usr/include/c++/4.9/ostream:513:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/4.9/iterator:66:0,
from ctest.cpp:6:
/usr/include/c++/4.9/bits/stream_iterator.h:198:13: note: mismatched types ‘const _CharT*’ and ‘std::vector<int>’
*_M_stream << __value;
^
In file included from /usr/include/c++/4.9/ostream:612:0,
from /usr/include/c++/4.9/iostream:39,
from ctest.cpp:1:
/usr/include/c++/4.9/bits/ostream.tcc:321:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const char*)
operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
^
/usr/include/c++/4.9/bits/ostream.tcc:321:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/4.9/iterator:66:0,
from ctest.cpp:6:
/usr/include/c++/4.9/bits/stream_iterator.h:198:13: note: cannot convert ‘__value’ (type ‘const std::vector<int>’) to type ‘const char*’
*_M_stream << __value;
^
In file included from /usr/include/c++/4.9/iostream:39:0,
from ctest.cpp:1:
/usr/include/c++/4.9/ostream:530:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const char*)
operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
^
/usr/include/c++/4.9/ostream:530:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/4.9/iterator:66:0,
from ctest.cpp:6:
/usr/include/c++/4.9/bits/stream_iterator.h:198:13: note: cannot convert ‘__value’ (type ‘const std::vector<int>’) to type ‘const char*’
*_M_stream << __value;
^
In file included from /usr/include/c++/4.9/iostream:39:0,
from ctest.cpp:1:
/usr/include/c++/4.9/ostream:543:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const signed char*)
operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
^
/usr/include/c++/4.9/ostream:543:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/4.9/iterator:66:0,
from ctest.cpp:6:
/usr/include/c++/4.9/bits/stream_iterator.h:198:13: note: cannot convert ‘__value’ (type ‘const std::vector<int>’) to type ‘const signed char*’
*_M_stream << __value;
^
In file included from /usr/include/c++/4.9/iostream:39:0,
from ctest.cpp:1:
/usr/include/c++/4.9/ostream:548:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const unsigned char*)
operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
^
/usr/include/c++/4.9/ostream:548:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/4.9/iterator:66:0,
from ctest.cpp:6:
/usr/include/c++/4.9/bits/stream_iterator.h:198:13: note: cannot convert ‘__value’ (type ‘const std::vector<int>’) to type ‘const unsigned char*’
*_M_stream << __value;
^
And this is the full file:
template <typename T>
std::ostream& operator << (std::ostream& out, const std::vector<T>& v){
if(! v.empty()){
std::cout<< '[';
std::copy(v.begin(), v.end(), std::ostream_iterator<T>(std::cout, " "));
std::cout<< ']' << std::endl;
}
else{
std::cout << "[ ]" << std::endl;
}
}
int main()
{
std::ifstream infile("filetest.txt");
std::string output;
std::string line;
std::vector<std::vector<int> > vec;
while(std::getline(infile, line))
{
std::vector<int> subvec;
int i;
std::istringstream iss(line);
while(iss >> i)
{
subvec.push_back(i);
if(iss.peek()==','|| iss.peek()== ' ')
iss.ignore();
}
std::cout << subvec;
vec.push_back(subvec);
}
std::cout << vec;
}
It depends heavily upon how you want your output formatted. You can just use the operator as-is, and it'll work (though you may or may not like the formatting):
#include <vector>
using std::vector;
#include <iostream>
using std::ostream;
template<typename T>
ostream& operator<< (ostream& out, const vector<T>& v) {
out << "[";
size_t last = v.size() - 1;
for (size_t i = 0; i < v.size(); ++i) {
out << v[i];
if (i != last)
out << ", ";
}
out << "]";
return out;
}
int main() {
std::vector<std::vector<int> > x { { 1, 2, 3 }, { 4, 5, 6 } };
std::cout << x;
}
Produces:
[[1, 2, 3], [4, 5, 6]]
Things only get more difficult if you need/want to change the formatting between one instance and another, such as having commas between the numbers, and new-lines between the rows. This is somewhat non-trivial to solve well, but I'll leave it at this for now, since it's not at all clear you really even want that.
This idea uses std::enable_if to iterate through members of what we choose to define as is_container. I wrote the code more out of curiosity than out of practicality though.
#include <iostream>
#include <type_traits>
#include <vector>
// by default you declare that no type is a container
template<typename T>
struct is_container {
static constexpr bool value = false;
};
// then you specify which types are actually containers.. I'm just declaring
// std::vector<T, A>, but you could make a similar declaration for list, array,
// and so on.
template<typename T, typename Allocator>
struct is_container<std::vector<T, Allocator> > {
static constexpr bool value = true;
};
// then you enable a print function to print all elements in a container (you
// can modify the actual formatting to your liking)
template<typename T>
typename std::enable_if<
is_container<T>::value,
std::ostream& >::type
inline operator<<(std::ostream& os, const T& container) {
const auto N = container.size();
std::size_t current = 0;
os << "[";
for(const auto & item : container) {
const char* separator = current++ == N - 1 ? "" : ", ";
os << item << separator;
}
os << "]";
return os;
}
// then the code will work for any level of nesting
int main() {
int scalar = 1;
std::cout << "Scalar: " << scalar << std::endl;
std::vector<int> vector {1, 2, 3, 4};
std::cout << "Vector: " << vector << std::endl;
std::vector< std::vector<int> > vvector {
{1, 2, 3, 4},
{5, 6, 7},
{8, 9, 10, 11, 12}
};
std::cout << "Vector<Vector>: " << vvector << std::endl;
std::vector< std::vector< std::vector<int> > > vvvector {
{
{1, 2, 3, 4},
{5, 6, 7},
{8, 9, 10, 11, 12}
},
{
{-1, -2, -3, -4},
{-5, -6, -7},
{-8, -9, -10}
}
};
std::cout << "Vector<Vector<Vector>>: " << vvvector << std::endl;
// you get the idea...
return 0;
}
Sample run:
$ g++ example.cpp -std=c++14 -Wall -Wextra -O3
$ ./a.out
Scalar: 1
Vector: [1, 2, 3, 4]
Vector<Vector>: [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10, 11, 12]]
Vector<Vector<Vector>>: [[[1, 2, 3, 4], [5, 6, 7], [8, 9, 10, 11, 12]], [[-1, -2, -3, -4], [-5, -6, -7], [-8, -9, -10]]]

SFINAE return type overload

I am trying to have two overloads of same function called something. This function should take another function as parameter, and it should be overload based on return type of this other function. So far I have this:
#include <iostream>
#include <type_traits>
using namespace std;
unsigned long function1() {
return 5;
}
void function2() {
return;
}
template <typename Algorithm, typename enable_if<is_void<decltype(Algorithm())>::value>::type* = nullptr>
void something(Algorithm a) {
std::cout << "algorithm returns void\n";
}
template <typename Algorithm, typename enable_if<!is_void<decltype(Algorithm())>::value>::type* = nullptr>
decltype(Algorithm()) something(Algorithm a) {
std::cout << "algorithm returns non-void: " << a() << "\n";
}
int main() {
something(function1);
something(function2);
}
However, this does not compile. Here is build message I get with gcc compiler:
||=== Build: Debug in Library (compiler: GNU GCC Compiler) ===|
C:\Alex\Programming\C++\Library\test.cpp||In instantiation of 'decltype (Algorithm()) something(Algorithm) [with Algorithm = long unsigned int (*)(); typename std::enable_if<(! std::is_void<decltype (Algorithm())>::value), void>::type* <anonymous> = 0u; decltype (Algorithm()) = long unsigned int (*)()]':|
C:\Alex\Programming\C++\Library\test.cpp|23|required from here|
C:\Alex\Programming\C++\Library\test.cpp|20|warning: no return statement in function returning non-void [-Wreturn-type]|
C:\Alex\Programming\C++\Library\test.cpp||In instantiation of 'decltype (Algorithm()) something(Algorithm) [with Algorithm = void (*)(); typename std::enable_if<(! std::is_void<decltype (Algorithm())>::value), void>::type* <anonymous> = 0u; decltype (Algorithm()) = void (*)()]':|
C:\Alex\Programming\C++\Library\test.cpp|24|required from here|
C:\Alex\Programming\C++\Library\test.cpp|19|error: no match for 'operator<<' in 'std::operator<< <std::char_traits<char> >((* & std::cout), ((const char*)"algorithm returns non-void: ")) << a()'|
C:\Alex\Programming\C++\Library\test.cpp|19|note: candidates are:|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|106|note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ostream_type& (*)(std::basic_ostream<_CharT, _Traits>::__ostream_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|106|note: no known conversion for argument 1 from 'void' to 'std::basic_ostream<char>::__ostream_type& (*)(std::basic_ostream<char>::__ostream_type&) {aka std::basic_ostream<char>& (*)(std::basic_ostream<char>&)}'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|115|note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ios_type& (*)(std::basic_ostream<_CharT, _Traits>::__ios_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>; std::basic_ostream<_CharT, _Traits>::__ios_type = std::basic_ios<char>]|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|115|note: no known conversion for argument 1 from 'void' to 'std::basic_ostream<char>::__ios_type& (*)(std::basic_ostream<char>::__ios_type&) {aka std::basic_ios<char>& (*)(std::basic_ios<char>&)}'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|125|note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|125|note: no known conversion for argument 1 from 'void' to 'std::ios_base& (*)(std::ios_base&)'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|164|note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|164|note: no known conversion for argument 1 from 'void' to 'long int'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|168|note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|168|note: no known conversion for argument 1 from 'void' to 'long unsigned int'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|172|note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|172|note: no known conversion for argument 1 from 'void' to 'bool'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\ostream.tcc|93|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char; _Traits = std::char_traits<char>]|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\ostream.tcc|93|note: no known conversion for argument 1 from 'void' to 'short int'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|179|note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|179|note: no known conversion for argument 1 from 'void' to 'short unsigned int'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\ostream.tcc|107|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char; _Traits = std::char_traits<char>]|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\ostream.tcc|107|note: no known conversion for argument 1 from 'void' to 'int'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|190|note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|190|note: no known conversion for argument 1 from 'void' to 'unsigned int'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|199|note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|199|note: no known conversion for argument 1 from 'void' to 'long long int'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|203|note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|203|note: no known conversion for argument 1 from 'void' to 'long long unsigned int'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|218|note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|218|note: no known conversion for argument 1 from 'void' to 'double'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|222|note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|222|note: no known conversion for argument 1 from 'void' to 'float'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|230|note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|230|note: no known conversion for argument 1 from 'void' to 'long double'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|243|note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|243|note: no known conversion for argument 1 from 'void' to 'const void*'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\ostream.tcc|121|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__streambuf_type*) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\ostream.tcc|121|note: no known conversion for argument 1 from 'void' to 'std::basic_ostream<char>::__streambuf_type* {aka std::basic_streambuf<char>*}'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.h|2750|note: template<class _CharT, class _Traits, class _Alloc> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&)|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.h|2750|note: template argument deduction/substitution failed:|
C:\Alex\Programming\C++\Library\test.cpp|19|note: mismatched types 'const std::basic_string<_CharT, _Traits, _Alloc>' and 'void'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|469|note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, _CharT)|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|469|note: template argument deduction/substitution failed:|
C:\Alex\Programming\C++\Library\test.cpp|19|note: deduced conflicting types for parameter '_CharT' ('char' and 'void')|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|474|note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, char)|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|474|note: template argument deduction/substitution failed:|
C:\Alex\Programming\C++\Library\test.cpp|19|note: cannot convert 'a()' (type 'void') to type 'char'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|480|note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, char)|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|480|note: template argument deduction/substitution failed:|
C:\Alex\Programming\C++\Library\test.cpp|19|note: cannot convert 'a()' (type 'void') to type 'char'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|486|note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, signed char)|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|486|note: template argument deduction/substitution failed:|
C:\Alex\Programming\C++\Library\test.cpp|19|note: cannot convert 'a()' (type 'void') to type 'signed char'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|491|note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, unsigned char)|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|491|note: template argument deduction/substitution failed:|
C:\Alex\Programming\C++\Library\test.cpp|19|note: cannot convert 'a()' (type 'void') to type 'unsigned char'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|511|note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const _CharT*)|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|511|note: template argument deduction/substitution failed:|
C:\Alex\Programming\C++\Library\test.cpp|19|note: mismatched types 'const _CharT*' and 'void'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\ostream.tcc|323|note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const char*)|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\ostream.tcc|323|note: template argument deduction/substitution failed:|
C:\Alex\Programming\C++\Library\test.cpp|19|note: cannot convert 'a()' (type 'void') to type 'const char*'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|528|note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const char*)|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|528|note: template argument deduction/substitution failed:|
C:\Alex\Programming\C++\Library\test.cpp|19|note: cannot convert 'a()' (type 'void') to type 'const char*'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|541|note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const signed char*)|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|541|note: template argument deduction/substitution failed:|
C:\Alex\Programming\C++\Library\test.cpp|19|note: cannot convert 'a()' (type 'void') to type 'const signed char*'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|546|note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const unsigned char*)|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|546|note: template argument deduction/substitution failed:|
C:\Alex\Programming\C++\Library\test.cpp|19|note: cannot convert 'a()' (type 'void') to type 'const unsigned char*'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|600|note: template<class _CharT, class _Traits, class _Tp> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&)|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|600|note: template argument deduction/substitution failed:|
C:\Alex\Programming\C++\Library\test.cpp|19|required from 'decltype (Algorithm()) something(Algorithm) [with Algorithm = void (*)(); typename std::enable_if<(! std::is_void<decltype (Algorithm())>::value), void>::type* <anonymous> = 0u; decltype (Algorithm()) = void (*)()]'|
C:\Alex\Programming\C++\Library\test.cpp|24|required from here|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|600|error: forming reference to void|
C:\Alex\Programming\C++\Library\test.cpp||In instantiation of 'decltype (Algorithm()) something(Algorithm) [with Algorithm = void (*)(); typename std::enable_if<(! std::is_void<decltype (Algorithm())>::value), void>::type* <anonymous> = 0u; decltype (Algorithm()) = void (*)()]':|
C:\Alex\Programming\C++\Library\test.cpp|24|required from here|
C:\Alex\Programming\C++\Library\test.cpp|20|warning: no return statement in function returning non-void [-Wreturn-type]|
||=== Build failed: 2 error(s), 10 warning(s) (0 minute(s), 2 second(s)) ===|
Is what I am trying to achieve possible in c++? If so, how?
Algorithm doesn't call the function but create an instance of the deduced type of the function. You need another pair of parentheses in order to call it
decltype(Algorithm()())
or even:
std::declval<Algorithm>()()
if your type is a functor with an inaccessible default-constructor.

Why this template recursion won't compile?

I am attempting to print out the numbers from 1 to 1000 in C++ without any control statements or conditionals. I have found this question + answer on SO, but I cannot compile it on Linux with gcc 4.8. Any ideas why this is not working?
This is the code I'm trying:
#include <iostream>
template<int N>
struct NumberGeneration{
static void out(std::ostream& os)
{
NumberGeneration<N-1>::out(os);
os << N << std::endl;
}
};
template<>
struct NumberGeneration<1>{
static void out(std::ostream& os)
{
os << 1 << std::endl;
}
};
int main(){
NumberGeneration<1000>::out(std::cout);
}
The error I get is this:
main2.cpp:9:34: error: template instantiation depth exceeds maximum of 900 (use -ftemplate-depth= to increase the maximum) instantiating ‘struct NumberGeneration<100>’
NumberGeneration<N-1>::out(os);
^
main2.cpp:9:34: recursively required from ‘static void NumberGeneration<N>::out(std::ostream&) [with int N = 999; std::ostream = std::basic_ostream<char>]’
main2.cpp:9:34: required from ‘static void NumberGeneration<N>::out(std::ostream&) [with int N = 1000; std::ostream = std::basic_ostream<char>]’
main2.cpp:21:28: required from here
main2.cpp:9:34: error: incomplete type ‘NumberGeneration<100>’ used in nested name specifier
main2.cpp:10:8: error: template instantiation depth exceeds maximum of 900 (use -ftemplate-depth= to increase the maximum) substituting ‘template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, char) [with _CharT = char; _Traits = std::char_traits<char>]’
os << N << std::endl;
^
main2.cpp:9:34: recursively required from ‘static void NumberGeneration<N>::out(std::ostream&) [with int N = 999; std::ostream = std::basic_ostream<char>]’
main2.cpp:9:34: required from ‘static void NumberGeneration<N>::out(std::ostream&) [with int N = 1000; std::ostream = std::basic_ostream<char>]’
main2.cpp:21:28: required from here
main2.cpp:10:8: error: template instantiation depth exceeds maximum of 900 (use -ftemplate-depth= to increase the maximum) substituting ‘template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, char) [with _Traits = std::char_traits<char>]’
main2.cpp:9:34: recursively required from ‘static void NumberGeneration<N>::out(std::ostream&) [with int N = 999; std::ostream = std::basic_ostream<char>]’
main2.cpp:9:34: required from ‘static void NumberGeneration<N>::out(std::ostream&) [with int N = 1000; std::ostream = std::basic_ostream<char>]’
main2.cpp:21:28: required from here
main2.cpp:10:8: error: template instantiation depth exceeds maximum of 900 (use -ftemplate-depth= to increase the maximum) substituting ‘template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, signed char) [with _Traits = std::char_traits<char>]’
main2.cpp:9:34: recursively required from ‘static void NumberGeneration<N>::out(std::ostream&) [with int N = 999; std::ostream = std::basic_ostream<char>]’
main2.cpp:9:34: required from ‘static void NumberGeneration<N>::out(std::ostream&) [with int N = 1000; std::ostream = std::basic_ostream<char>]’
main2.cpp:21:28: required from here
main2.cpp:10:8: error: template instantiation depth exceeds maximum of 900 (use -ftemplate-depth= to increase the maximum) substituting ‘template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, unsigned char) [with _Traits = std::char_traits<char>]’
main2.cpp:9:34: recursively required from ‘static void NumberGeneration<N>::out(std::ostream&) [with int N = 999; std::ostream = std::basic_ostream<char>]’
main2.cpp:9:34: required from ‘static void NumberGeneration<N>::out(std::ostream&) [with int N = 1000; std::ostream = std::basic_ostream<char>]’
main2.cpp:21:28: required from here
main2.cpp:10:13: error: template instantiation depth exceeds maximum of 900 (use -ftemplate-depth= to increase the maximum) substituting ‘template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, _CharT) [with _CharT = char; _Traits = std::char_traits<char>]’
os << N << std::endl;
^
main2.cpp:9:34: recursively required from ‘static void NumberGeneration<N>::out(std::ostream&) [with int N = 999; std::ostream = std::basic_ostream<char>]’
main2.cpp:9:34: required from ‘static void NumberGeneration<N>::out(std::ostream&) [with int N = 1000; std::ostream = std::basic_ostream<char>]’
main2.cpp:21:28: required from here
main2.cpp:10:13: error: template instantiation depth exceeds maximum of 900 (use -ftemplate-depth= to increase the maximum) substituting ‘template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const _CharT*) [with _CharT = char; _Traits = std::char_traits<char>]’
main2.cpp:9:34: recursively required from ‘static void NumberGeneration<N>::out(std::ostream&) [with int N = 999; std::ostream = std::basic_ostream<char>]’
main2.cpp:9:34: required from ‘static void NumberGeneration<N>::out(std::ostream&) [with int N = 1000; std::ostream = std::basic_ostream<char>]’
main2.cpp:21:28: required from here
main2.cpp:10:13: error: template instantiation depth exceeds maximum of 900 (use -ftemplate-depth= to increase the maximum) substituting ‘template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::endl(std::basic_ostream<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits<char>]’
main2.cpp:9:34: recursively required from ‘static void NumberGeneration<N>::out(std::ostream&) [with int N = 999; std::ostream = std::basic_ostream<char>]’
main2.cpp:9:34: required from ‘static void NumberGeneration<N>::out(std::ostream&) [with int N = 1000; std::ostream = std::basic_ostream<char>]’
main2.cpp:21:28: required from here
main2.cpp:10:13: error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’ and ‘<unresolved overloaded function type>’)
main2.cpp:10:13: note: candidates are:
In file included from /usr/include/c++/4.8/iostream:39:0,
from main2.cpp:1:
/usr/include/c++/4.8/ostream:108:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ostream_type& (*)(std::basic_ostream<_CharT, _Traits>::__ostream_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(__ostream_type& (*__pf)(__ostream_type&))
^
/usr/include/c++/4.8/ostream:108:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘std::basic_ostream<char>::__ostream_type& (*)(std::basic_ostream<char>::__ostream_type&) {aka std::basic_ostream<char>& (*)(std::basic_ostream<char>&)}’
/usr/include/c++/4.8/ostream:117:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ios_type& (*)(std::basic_ostream<_CharT, _Traits>::__ios_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>; std::basic_ostream<_CharT, _Traits>::__ios_type = std::basic_ios<char>]
operator<<(__ios_type& (*__pf)(__ios_type&))
^
/usr/include/c++/4.8/ostream:117:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘std::basic_ostream<char>::__ios_type& (*)(std::basic_ostream<char>::__ios_type&) {aka std::basic_ios<char>& (*)(std::basic_ios<char>&)}’
/usr/include/c++/4.8/ostream:127:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(ios_base& (*__pf) (ios_base&))
^
/usr/include/c++/4.8/ostream:127:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘std::ios_base& (*)(std::ios_base&)’
/usr/include/c++/4.8/ostream:166:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(long __n)
^
/usr/include/c++/4.8/ostream:166:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘long int’
/usr/include/c++/4.8/ostream:170:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(unsigned long __n)
^
/usr/include/c++/4.8/ostream:170:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘long unsigned int’
/usr/include/c++/4.8/ostream:174:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(bool __n)
^
/usr/include/c++/4.8/ostream:174:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘bool’
In file included from /usr/include/c++/4.8/ostream:609:0,
from /usr/include/c++/4.8/iostream:39,
from main2.cpp:1:
/usr/include/c++/4.8/bits/ostream.tcc:91:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char; _Traits = std::char_traits<char>]
basic_ostream<_CharT, _Traits>::
^
/usr/include/c++/4.8/bits/ostream.tcc:91:5: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘short int’
In file included from /usr/include/c++/4.8/iostream:39:0,
from main2.cpp:1:
/usr/include/c++/4.8/ostream:181:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(unsigned short __n)
^
/usr/include/c++/4.8/ostream:181:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘short unsigned int’
In file included from /usr/include/c++/4.8/ostream:609:0,
from /usr/include/c++/4.8/iostream:39,
from main2.cpp:1:
/usr/include/c++/4.8/bits/ostream.tcc:105:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char; _Traits = std::char_traits<char>]
basic_ostream<_CharT, _Traits>::
^
/usr/include/c++/4.8/bits/ostream.tcc:105:5: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘int’
In file included from /usr/include/c++/4.8/iostream:39:0,
from main2.cpp:1:
/usr/include/c++/4.8/ostream:192:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(unsigned int __n)
^
/usr/include/c++/4.8/ostream:192:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘unsigned int’
/usr/include/c++/4.8/ostream:201:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(long long __n)
^
/usr/include/c++/4.8/ostream:201:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘long long int’
/usr/include/c++/4.8/ostream:205:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(unsigned long long __n)
^
/usr/include/c++/4.8/ostream:205:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘long long unsigned int’
/usr/include/c++/4.8/ostream:220:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(double __f)
^
/usr/include/c++/4.8/ostream:220:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘double’
/usr/include/c++/4.8/ostream:224:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(float __f)
^
/usr/include/c++/4.8/ostream:224:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘float’
/usr/include/c++/4.8/ostream:232:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(long double __f)
^
/usr/include/c++/4.8/ostream:232:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘long double’
/usr/include/c++/4.8/ostream:245:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(const void* __p)
^
/usr/include/c++/4.8/ostream:245:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘const void*’
In file included from /usr/include/c++/4.8/ostream:609:0,
from /usr/include/c++/4.8/iostream:39,
from main2.cpp:1:
/usr/include/c++/4.8/bits/ostream.tcc:119:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__streambuf_type*) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]
basic_ostream<_CharT, _Traits>::
^
/usr/include/c++/4.8/bits/ostream.tcc:119:5: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘std::basic_ostream<char>::__streambuf_type* {aka std::basic_streambuf<char>*}’
In file included from /usr/include/c++/4.8/iostream:39:0,
from main2.cpp:1:
/usr/include/c++/4.8/ostream:548:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const unsigned char*)
operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
^
/usr/include/c++/4.8/ostream:548:5: note: template argument deduction/substitution failed:
main2.cpp:10:13: note: cannot convert ‘std::endl’ (type ‘<unresolved overloaded function type>’) to type ‘const unsigned char*’
os << N << std::endl;
^
In file included from /usr/include/c++/4.8/iostream:39:0,
from main2.cpp:1:
/usr/include/c++/4.8/ostream:543:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const signed char*)
operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
^
/usr/include/c++/4.8/ostream:543:5: note: template argument deduction/substitution failed:
main2.cpp:10:13: note: cannot convert ‘std::endl’ (type ‘<unresolved overloaded function type>’) to type ‘const signed char*’
os << N << std::endl;
^
In file included from /usr/include/c++/4.8/iostream:39:0,
from main2.cpp:1:
/usr/include/c++/4.8/ostream:530:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const char*)
operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
^
/usr/include/c++/4.8/ostream:530:5: note: template argument deduction/substitution failed:
main2.cpp:10:13: note: cannot convert ‘std::endl’ (type ‘<unresolved overloaded function type>’) to type ‘const char*’
os << N << std::endl;
^
In file included from /usr/include/c++/4.8/ostream:609:0,
from /usr/include/c++/4.8/iostream:39,
from main2.cpp:1:
/usr/include/c++/4.8/bits/ostream.tcc:321:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const char*)
operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
^
/usr/include/c++/4.8/bits/ostream.tcc:321:5: note: template argument deduction/substitution failed:
main2.cpp:10:13: note: cannot convert ‘std::endl’ (type ‘<unresolved overloaded function type>’) to type ‘const char*’
os << N << std::endl;
^
In file included from /usr/include/c++/4.8/iostream:39:0,
from main2.cpp:1:
/usr/include/c++/4.8/ostream:513:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const _CharT*)
operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
^
/usr/include/c++/4.8/ostream:513:5: note: substitution of deduced template arguments resulted in errors seen above
/usr/include/c++/4.8/ostream:493:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, unsigned char)
operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
^
/usr/include/c++/4.8/ostream:493:5: note: template argument deduction/substitution failed:
main2.cpp:10:13: note: cannot convert ‘std::endl’ (type ‘<unresolved overloaded function type>’) to type ‘unsigned char’
os << N << std::endl;
^
In file included from /usr/include/c++/4.8/iostream:39:0,
from main2.cpp:1:
/usr/include/c++/4.8/ostream:488:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, signed char)
operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
^
/usr/include/c++/4.8/ostream:488:5: note: template argument deduction/substitution failed:
main2.cpp:10:13: note: cannot convert ‘std::endl’ (type ‘<unresolved overloaded function type>’) to type ‘signed char’
os << N << std::endl;
^
In file included from /usr/include/c++/4.8/iostream:39:0,
from main2.cpp:1:
/usr/include/c++/4.8/ostream:482:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, char)
operator<<(basic_ostream<char, _Traits>& __out, char __c)
^
/usr/include/c++/4.8/ostream:482:5: note: template argument deduction/substitution failed:
main2.cpp:10:13: note: cannot convert ‘std::endl’ (type ‘<unresolved overloaded function type>’) to type ‘char’
os << N << std::endl;
^
In file included from /usr/include/c++/4.8/iostream:39:0,
from main2.cpp:1:
/usr/include/c++/4.8/ostream:476:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, char)
operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
^
/usr/include/c++/4.8/ostream:476:5: note: template argument deduction/substitution failed:
main2.cpp:10:13: note: cannot convert ‘std::endl’ (type ‘<unresolved overloaded function type>’) to type ‘char’
os << N << std::endl;
^
In file included from /usr/include/c++/4.8/iostream:39:0,
from main2.cpp:1:
/usr/include/c++/4.8/ostream:471:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, _CharT)
operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
^
/usr/include/c++/4.8/ostream:471:5: note: substitution of deduced template arguments resulted in errors seen above
In file included from /usr/include/c++/4.8/string:52:0,
from /usr/include/c++/4.8/bits/locale_classes.h:40,
from /usr/include/c++/4.8/bits/ios_base.h:41,
from /usr/include/c++/4.8/ios:42,
from /usr/include/c++/4.8/ostream:38,
from /usr/include/c++/4.8/iostream:39,
from main2.cpp:1:
/usr/include/c++/4.8/bits/basic_string.h:2753:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
/usr/include/c++/4.8/bits/basic_string.h:2753:5: note: template argument deduction/substitution failed:
main2.cpp:10:13: note: couldn't deduce template parameter ‘_Alloc’
os << N << std::endl;
^
And when I increase template instantiation depth I get :
$ gcc -Wall -ftemplate-depth=1100 -omain2 main2.cpp
/tmp/ccI81cmF.o: In function `main':
main2.cpp:(.text+0x5): undefined reference to `std::cout'
/tmp/ccI81cmF.o: In function `__static_initialization_and_destruction_0(int, int)':
main2.cpp:(.text+0x38): undefined reference to `std::ios_base::Init::Init()'
main2.cpp:(.text+0x47): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccI81cmF.o: In function `NumberGeneration<1>::out(std::ostream&)':
main2.cpp:(.text._ZN16NumberGenerationILi1EE3outERSo[_ZN16NumberGenerationILi1EE3outERSo]+0x19): undefined reference to `std::ostream::operator<<(int)'
main2.cpp:(.text._ZN16NumberGenerationILi1EE3outERSo[_ZN16NumberGenerationILi1EE3outERSo]+0x1e): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
main2.cpp:(.text._ZN16NumberGenerationILi1EE3outERSo[_ZN16NumberGenerationILi1EE3outERSo]+0x26): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
/tmp/ccI81cmF.o: In function `NumberGeneration<1000>::out(std::ostream&)':
main2.cpp:(.text._ZN16NumberGenerationILi1000EE3outERSo[_ZN16NumberGenerationILi1000EE3outERSo]+0x25): undefined reference to `std::ostream::operator<<(int)'
main2.cpp:(.text._ZN16NumberGenerationILi1000EE3outERSo[_ZN16NumberGenerationILi1000EE3outERSo]+0x2a): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
main2.cpp:(.text._ZN16NumberGenerationILi1000EE3outERSo[_ZN16NumberGenerationILi1000EE3outERSo]+0x32): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
As state in the comment, you reach the template depth limit, and use the wrong compiler:
so use a C++ compiler as g++
And then you may pass this argument to increase depth limit -depth=1000
or you may instantiate intermediate template as
template struct NumberGeneration<500>;
or rewrite your template to not use a depth linear with N
something like the following
// index_sequence and make_index_sequence available in C++14, but not in C++11
template <std::size_t...> struct index_sequence {};
namespace detail
{
template <typename, typename> struct concat;
template <std::size_t... Is1, std::size_t... Is2>
struct concat<index_sequence<Is1...>, index_sequence<Is2...>> :
index_sequence<Is1..., (sizeof...(Is1) + Is2)...>
{
using type = index_sequence<Is1..., (sizeof...(Is1) + Is2)...>;
};
}
template <std::size_t N>
struct make_index_sequence :
detail::concat<typename make_index_sequence<N / 2>::type,
typename make_index_sequence<N - N / 2>::type>::type
{
using type = typename
detail::concat<typename make_index_sequence<N / 2>::type,
typename make_index_sequence<N - N / 2>::type>::type;
};
template <>
struct make_index_sequence<1u> : index_sequence<0u> {
using type = index_sequence<0u>;
};
template <>
struct make_index_sequence<0u> : index_sequence<> {
using type = index_sequence<>;
};
And then with variadic template:
template<int N>
struct NumberGeneration{
static void out(std::ostream& os)
{
out(make_index_sequence<N>(), os);
}
private:
template<std::size_t ...Is>
static void out(index_sequence<Is...>, std::ostream& os)
{
std::initializer_list<int>{ ((os << (1 + Is) << std::endl), void(), 0)...};
}
};
Live example
The big problem is that it is really bad C++. It has a 1000 deep template instantiation depth, and the C++11 standard only advises 900. What more, there is little to no need for something that deep to do such a simple task.
Most compilers have template instantiation depth modification flags that make it larger. That is, however, a patch over the problem. The right way to fix that code is to either not write it, or decrease the depth.
#include <iostream>
template<int Max, unsigned Count=Max>
struct NumberGeneration{
void operator()(std::ostream& os) const
{
NumberGeneration<Max, (Count+1)/2>()(os);
NumberGeneration<Max-(Count+1)/2, (Count)/2>()(os);
}
};
template<int Max>
struct NumberGeneration<Max, 1>{
void operator()(std::ostream& os) const
{
os << Max << std::endl;
}
};
// not required:
template<int Max>
struct NumberGeneration<Max, 0>{
void operator()(std::ostream& os) const
{}
};
int main(){
NumberGeneration<1000>()(std::cout);
}
This now has a recursive template instantiation depth of about 10 instead of 1000, and is nearly as simple as the original version. I simply replaced single recursion with divide-and-conquer.
In C++14, efficient lists of integers via std::integral_sequence exist, and allow the above kind of thing even easier.
I also made NumberGeneration<X> into a stateless invokable type instead of having a static void out() method.

no match for 'operator<<' in 'std::cout << ::operator++(int)(0)'

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <sstream>
#include <typeinfo>
#define debug(args...) // Just strip off all debug tokens
using namespace std;
// CUT begin
#define debug(args...) {dbg,args;cout<<endl;}
struct debugger{template<typename T> debugger& operator ,(const T& v){cout<<v<<" ";return *this;}}dbg;
template <typename T1,typename T2> inline ostream& operator<<(ostream& os,const pair<T1,T2>& p){return os<<"("<<p.first<<", "<<p.second<<")";}
template<typename T>inline ostream&operator<<(ostream& os,const vector<T>& v){string delim="[";for(unsigned int i=0;i < v.size();i++){os<<delim<<v[i];delim=", ";}return os<<"]";}
template<typename T>inline ostream&operator<<(ostream& os,const set<T>& v){string delim="[";for (typename set<T>::const_iterator ii=v.begin();ii!=v.end();++ii){os<<delim<<*ii;delim=", ";}return os<<"]";}
template<typename T1,typename T2>inline ostream&operator<<(ostream& os,const map<T1,T2>& v){string delim="[";for (typename map<T1,T2>::const_iterator ii=v.begin();ii!=v.end();++ii){os<<delim<<*ii;delim=", ";}return os<<"]";}
// CUT end
class Vehicle
{
public:
int n;
Vehicle(int n):n(n){cout<<"Ctor Vehicle "<<n<<endl;}
Vehicle(Vehicle& v):n(v.n){cout<<"Copy Ctor Vehicle "<<n<<endl;}
virtual ~Vehicle(){cout<<"Dtor Vehicle "<<n<<endl;}
virtual ostream& dump(ostream& os){return os<<"Vehicle("<<n<<")";}
string to_str(){stringstream s; dump(s); return s.str();}
Vehicle& operator++(){n++;return *this;}
Vehicle operator++(int x){Vehicle v(*this); operator++(); return v;}
};
class Car: public Vehicle
{
public:
Car(int n): Vehicle(n){cout<<"Ctor Car "<<n<<endl;}
virtual ~Car(){cout<<"Dtor Car "<<n<<endl;}
virtual ostream& dump(ostream& os){return os<<"Car("<<n<<")";}
Car operator++(int x){Car v(*this); operator++(); return v;}
Car& operator++(){n++; return *this;}
/* data */
};
ostream& operator<<(ostream& os, Vehicle& v)
{
return v.dump(os);
}
int main(int argc, char const *argv[])
{
Car c(10);
// cout<<c++<<endl;
cout<<c++<<endl;
return 0;
}
the line cout<<c++<<endl; causes the following error:
C:\Users\Rajat\Documents\GitHub\interview-preparation\cpp_test.cpp:16:0: warning: "debug" redefined [enabled by default]
C:\Users\Rajat\Documents\GitHub\interview-preparation\cpp_test.cpp:13:0: note: this is the location of the previous definition
C:\Users\Rajat\Documents\GitHub\interview-preparation\cpp_test.cpp: In function 'int main(int, const char**)':
C:\Users\Rajat\Documents\GitHub\interview-preparation\cpp_test.cpp:57:9: error: no match for 'operator<<' in 'std::cout << Car::operator++(int)(0)'
C:\Users\Rajat\Documents\GitHub\interview-preparation\cpp_test.cpp:57:9: note: candidates are:
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:110:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ostream_type& (*)(std::basic_ostream<_CharT, _Traits>::__ostream_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:110:7: note: no known conversion for argument 1 from 'Car {aka Car}' to 'std::basic_ostream<char>::__ostream_type& (*)(std::basic_ostream<char>::__ostream_type&) {aka std::basic_ostream<char>& (*)(std::basic_ostream<char>&)}'
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:119:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ios_type& (*)(std::basic_ostream<_CharT, _Traits>::__ios_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>, std::basic_ostream<_CharT, _Traits>::__ios_type = std::basic_ios<char>]
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:119:7: note: no known conversion for argument 1 from 'Car {aka Car}' to 'std::basic_ostream<char>::__ios_type& (*)(std::basic_ostream<char>::__ios_type&) {aka std::basic_ios<char>& (*)(std::basic_ios<char>&)}'
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:129:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:129:7: note: no known conversion for argument 1 from 'Car {aka Car}' to 'std::ios_base& (*)(std::ios_base&)'
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:167:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:167:7: note: no known conversion for argument 1 from 'Car {aka Car}' to 'long int'
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:171:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:171:7: note: no known conversion for argument 1 from 'Car {aka Car}' to 'long unsigned int'
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:175:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:175:7: note: no known conversion for argument 1 from 'Car {aka Car}' to 'bool'
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/bits/ostream.tcc:93:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits<char>]
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/bits/ostream.tcc:93:5: note: no known conversion for argument 1 from 'Car {aka Car}' to 'short int'
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:182:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:182:7: note: no known conversion for argument 1 from 'Car {aka Car}' to 'short unsigned int'
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/bits/ostream.tcc:107:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/bits/ostream.tcc:107:5: note: no known conversion for argument 1 from 'Car {aka Car}' to 'int'
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:193:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:193:7: note: no known conversion for argument 1 from 'Car {aka Car}' to 'unsigned int'
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:202:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:202:7: note: no known conversion for argument 1 from 'Car {aka Car}' to 'long long int'
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:206:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:206:7: note: no known conversion for argument 1 from 'Car {aka Car}' to 'long long unsigned int'
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:211:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:211:7: note: no known conversion for argument 1 from 'Car {aka Car}' to 'double'
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:215:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:215:7: note: no known conversion for argument 1 from 'Car {aka Car}' to 'float'
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:223:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:223:7: note: no known conversion for argument 1 from 'Car {aka Car}' to 'long double'
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:227:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:227:7: note: no known conversion for argument 1 from 'Car {aka Car}' to 'const void*'
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/bits/ostream.tcc:121:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__streambuf_type*) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/bits/ostream.tcc:121:5: note: no known conversion for argument 1 from 'Car {aka Car}' to 'std::basic_ostream<char>::__streambuf_type* {aka std::basic_streambuf<char>*}'
C:\Users\Rajat\Documents\GitHub\interview-preparation\cpp_test.cpp:18:95: note: template<class T1, class T2> std::ostream& operator<<(std::ostream&, const std::pair<_T1, _T2>&)
C:\Users\Rajat\Documents\GitHub\interview-preparation\cpp_test.cpp:19:77: note: template<class T> std::ostream& operator<<(std::ostream&, const std::vector<T>&)
C:\Users\Rajat\Documents\GitHub\interview-preparation\cpp_test.cpp:20:74: note: template<class T> std::ostream& operator<<(std::ostream&, const std::set<T>&)
C:\Users\Rajat\Documents\GitHub\interview-preparation\cpp_test.cpp:21:91: note: template<class T1, class T2> std::ostream& operator<<(std::ostream&, const std::map<T1, T2>&)
C:\Users\Rajat\Documents\GitHub\interview-preparation\cpp_test.cpp:48:10: note: std::ostream& operator<<(std::ostream&, Vehicle&)
C:\Users\Rajat\Documents\GitHub\interview-preparation\cpp_test.cpp:48:10: note: no known conversion for argument 2 from 'Car {aka Car}' to 'Vehicle&'
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:528:77: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const unsigned char*)
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:523:75: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const signed char*)
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:510:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const char*)
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/bits/ostream.tcc:323:70: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const char*)
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:493:72: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const _CharT*)
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:473:70: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, unsigned char)
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:468:68: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, signed char)
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:462:61: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, char)
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:456:63: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, char)
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:451:65: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, _CharT)
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/bits/basic_string.h:2694:59: note: template<class _CharT, class _Traits, class _Alloc> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
[Finished in 1.2s with exit code 1]
Why is such an error occuring?
Your operator + returns a temporary of type Car, which cannot be bound to an lvalue reference (which is what your operator << accepts), since temporary objects are rvalues (more precisely, prvalues).
Change your insertion operator into:
ostream& operator<<(ostream& os, Vehicle const& v)
// ^^^^^
{
return v.dump(os);
}
Accordingly, make dump() a const member function. This all makes sense, since a function that is supposed to provide a representation of an object should not alter the state of that object:
class Vehicle
{
// ...
virtual ostream& dump(ostream& os) const {return os<<"Vehicle("<<n<<")";}
// ^^^^^
// ...
};
class Car: public Vehicle
{
// ...
virtual ostream& dump(ostream& os) const {return os<<"Car("<<n<<")";}
// ^^^^^
// ...
};