Why this template recursion won't compile? - c++

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.

Related

c++: compilation errors with std::endl and operator>>

I was writing a simple piece of c++ code to print out input till the input is not 42. Here is my code:
#include <iostream>
using namespace std;
int main(){
while(true){
int inp;
cin >> inp >> endl;
if(inp == 42){
break;
}
cout << inp << endl;
}
return 0;
}
However when I compile it ,I get a list of errors:
testlife.cpp: In function ‘int main()’:
testlife.cpp:9:14: error: no match for ‘operator>>’ (operand types are ‘std::basic_istream<char>::__istream_type {aka std::basic_istream<char>}’ and ‘<unresolved overloaded function type>’)
cin >> inp >> endl;
~~~~~~~~~~~^~~~~~~
In file included from /usr/include/c++/6/iostream:40:0,
from testlife.cpp:2:
/usr/include/c++/6/istream:120:7: note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__istream_type& (*)(std::basic_istream<_CharT, _Traits>::__istream_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
operator>>(__istream_type& (*__pf)(__istream_type&))
^~~~~~~~
/usr/include/c++/6/istream:120:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘std::basic_istream<char>::__istream_type& (*)(std::basic_istream<char>::__istream_type&) {aka std::basic_istream<char>& (*)(std::basic_istream<char>&)}’
/usr/include/c++/6/istream:124:7: note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__ios_type& (*)(std::basic_istream<_CharT, _Traits>::__ios_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>; std::basic_istream<_CharT, _Traits>::__ios_type = std::basic_ios<char>]
operator>>(__ios_type& (*__pf)(__ios_type&))
^~~~~~~~
/usr/include/c++/6/istream:124:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘std::basic_istream<char>::__ios_type& (*)(std::basic_istream<char>::__ios_type&) {aka std::basic_ios<char>& (*)(std::basic_ios<char>&)}’
/usr/include/c++/6/istream:131:7: note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::ios_base& (*)(std::ios_base&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
operator>>(ios_base& (*__pf)(ios_base&))
^~~~~~~~
/usr/include/c++/6/istream:131:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘std::ios_base& (*)(std::ios_base&)’
/usr/include/c++/6/istream:168:7: note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(bool&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
operator>>(bool& __n)
^~~~~~~~
/usr/include/c++/6/istream:168:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘bool&’
/usr/include/c++/6/istream:172:7: note: candidate: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short int&) [with _CharT = char; _Traits = std::char_traits<char>]
operator>>(short& __n);
^~~~~~~~
/usr/include/c++/6/istream:172:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘short int&’
/usr/include/c++/6/istream:175:7: note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(short unsigned int&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
operator>>(unsigned short& __n)
^~~~~~~~
/usr/include/c++/6/istream:175:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘short unsigned int&’
/usr/include/c++/6/istream:179:7: note: candidate: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(int&) [with _CharT = char; _Traits = std::char_traits<char>]
operator>>(int& __n);
^~~~~~~~
/usr/include/c++/6/istream:179:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘int&’
/usr/include/c++/6/istream:182:7: note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(unsigned int&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
operator>>(unsigned int& __n)
^~~~~~~~
/usr/include/c++/6/istream:182:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘unsigned int&’
/usr/include/c++/6/istream:186:7: note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long int&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
operator>>(long& __n)
^~~~~~~~
/usr/include/c++/6/istream:186:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘long int&’
/usr/include/c++/6/istream:190:7: note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long unsigned int&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
operator>>(unsigned long& __n)
^~~~~~~~
/usr/include/c++/6/istream:190:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘long unsigned int&’
/usr/include/c++/6/istream:195:7: note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long long int&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
operator>>(long long& __n)
^~~~~~~~
/usr/include/c++/6/istream:195:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘long long int&’
/usr/include/c++/6/istream:199:7: note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long long unsigned int&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
operator>>(unsigned long long& __n)
^~~~~~~~
/usr/include/c++/6/istream:199:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘long long unsigned int&’
/usr/include/c++/6/istream:214:7: note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(float&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
operator>>(float& __f)
^~~~~~~~
/usr/include/c++/6/istream:214:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘float&’
/usr/include/c++/6/istream:218:7: note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(double&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
operator>>(double& __f)
^~~~~~~~
/usr/include/c++/6/istream:218:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘double&’
/usr/include/c++/6/istream:222:7: note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long double&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
operator>>(long double& __f)
^~~~~~~~
/usr/include/c++/6/istream:222:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘long double&’
/usr/include/c++/6/istream:235:7: note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(void*&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
operator>>(void*& __p)
^~~~~~~~
/usr/include/c++/6/istream:235:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘void*&’
/usr/include/c++/6/istream:259:7: note: candidate: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__streambuf_type*) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]
operator>>(__streambuf_type* __sb);
^~~~~~~~
/usr/include/c++/6/istream:259:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘std::basic_istream<char>::__streambuf_type* {aka std::basic_streambuf<char>*}’
In file included from /usr/include/c++/6/string:53:0,
from /usr/include/c++/6/bits/locale_classes.h:40,
from /usr/include/c++/6/bits/ios_base.h:41,
from /usr/include/c++/6/ios:42,
from /usr/include/c++/6/ostream:38,
from /usr/include/c++/6/iostream:39,
from testlife.cpp:2:
/usr/include/c++/6/bits/basic_string.tcc:1437:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)
operator>>(basic_istream<_CharT, _Traits>& __in,
^~~~~~~~
/usr/include/c++/6/bits/basic_string.tcc:1437:5: note: template argument deduction/substitution failed:
testlife.cpp:9:17: note: couldn't deduce template parameter ‘_Alloc’
cin >> inp >> endl;
^~~~
In file included from /usr/include/c++/6/iostream:40:0,
from testlife.cpp:2:
/usr/include/c++/6/istream:799:5: note: candidate: std::basic_istream<_CharT2, _Traits2>& std::operator>>(std::basic_istream<_CharT2, _Traits2>&, _CharT2*) [with _CharT2 = char; _Traits2 = std::char_traits<char>; _CharT = char; _Traits = std::char_traits<char>]
operator>>(basic_istream<char>& __in, char* __s);
^~~~~~~~
/usr/include/c++/6/istream:799:5: note: no known conversion for argument 2 from ‘<unresolved overloaded function type>’ to ‘char*’
In file included from /usr/include/c++/6/istream:934:0,
from /usr/include/c++/6/iostream:40,
from testlife.cpp:2:
/usr/include/c++/6/bits/istream.tcc:923:5: note: candidate: std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT&) [with _CharT = char; _Traits = std::char_traits<char>]
operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c)
^~~~~~~~
/usr/include/c++/6/bits/istream.tcc:923:5: note: no known conversion for argument 2 from ‘<unresolved overloaded function type>’ to ‘char&’
In file included from /usr/include/c++/6/iostream:40:0,
from testlife.cpp:2:
/usr/include/c++/6/istream:756:5: note: candidate: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char&)
operator>>(basic_istream<char, _Traits>& __in, unsigned char& __c)
^~~~~~~~
/usr/include/c++/6/istream:756:5: note: template argument deduction/substitution failed:
testlife.cpp:9:17: note: cannot convert ‘std::endl’ (type ‘<unresolved overloaded function type>’) to type ‘unsigned char&’
cin >> inp >> endl;
^~~~
In file included from /usr/include/c++/6/iostream:40:0,
from testlife.cpp:2:
/usr/include/c++/6/istream:761:5: note: candidate: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char&)
operator>>(basic_istream<char, _Traits>& __in, signed char& __c)
^~~~~~~~
/usr/include/c++/6/istream:761:5: note: template argument deduction/substitution failed:
testlife.cpp:9:17: note: cannot convert ‘std::endl’ (type ‘<unresolved overloaded function type>’) to type ‘signed char&’
cin >> inp >> endl;
^~~~
In file included from /usr/include/c++/6/iostream:40:0,
from testlife.cpp:2:
/usr/include/c++/6/istream:803:5: note: candidate: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char*)
operator>>(basic_istream<char, _Traits>& __in, unsigned char* __s)
^~~~~~~~
/usr/include/c++/6/istream:803:5: note: template argument deduction/substitution failed:
testlife.cpp:9:17: note: cannot convert ‘std::endl’ (type ‘<unresolved overloaded function type>’) to type ‘unsigned char*’
cin >> inp >> endl;
^~~~
In file included from /usr/include/c++/6/iostream:40:0,
from testlife.cpp:2:
/usr/include/c++/6/istream:808:5: note: candidate: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char*)
operator>>(basic_istream<char, _Traits>& __in, signed char* __s)
^~~~~~~~
/usr/include/c++/6/istream:808:5: note: template argument deduction/substitution failed:
testlife.cpp:9:17: note: cannot convert ‘std::endl’ (type ‘<unresolved overloaded function type>’) to type ‘signed char*’
cin >> inp >> endl;
^~~~
In file included from /usr/include/c++/6/iostream:40:0,
from testlife.cpp:2:
/usr/include/c++/6/istream:924:5: note: candidate: template<class _CharT, class _Traits, class _Tp> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&&, _Tp&)
operator>>(basic_istream<_CharT, _Traits>&& __is, _Tp& __x)
^~~~~~~~
/usr/include/c++/6/istream:924:5: note: template argument deduction/substitution failed:
testlife.cpp:9:17: note: couldn't deduce template parameter ‘_Tp’
cin >> inp >> endl;
I have never encountered such error before nor I am unable to debug it given the simple block of code I am writing. I am using g++ compiler installed on ubuntu system.
Remove the endl from the cin statement:
cin >> inp;
Instead of:
cin >> inp >> endl;
There's no overload of the std::endl I/O manipulator for std::istream as the error message clearly states.
Also you should handle errors when taking input, otherwise your loop will run forever:
int main(){
while(true){
int inp;
if(cin >> inp && inp == 42) {
break;
}
else {
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
continue;
}
cout << inp << endl;
}
return 0;
}

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]]]

Using overloaded operator+ without assignment

I have simple(not simple) question: Is any possibility that overloaded operator+ will be working not as we want when we won't use assignment overloaded operator?
Example:
object a, b, c;
//Here is some code where I add some numbers to object a and b;
c=a+b;
cout << c;
And the thing is: when I'm using overloaded assingment operator, everything is fine. But without this, elements which are in c gets some random numbers. Why?
And another question: why I can't do something like that with overloaded << and + operators:
cout << a+b;
EDIT: Here is code:
#include <iostream>
#include <cstdio>
using namespace std;
class List;
class element{
private:
int number;
element* next;
friend class List;
friend ostream & operator<<(ostream &, List &);
public:
element(int data){
number=data;
}
int getData(){
return number;
}
};
class List{
private:
element* head;
int size;
friend ostream & operator<<(ostream &, List &);
public:
List(){
head=NULL;
size=0;
}
List(const List &lista){
//cout << "wyw" << endl;
//fflush(stdout);
if (lista.size==0){
size=0;
head=NULL;
}
else
{
size=0;
head=NULL;
element* tmp=lista.head;
while(tmp!=NULL){
cout << tmp->number << " ";
fflush(stdout);
this->addToList(tmp->number);
tmp=tmp->next;
}
}
}
~List(){
if (head!=NULL){
element* tmp=head;
element *temp;
while(tmp!=NULL){
temp=tmp->next;
delete tmp;
tmp=temp;
}
head=NULL;
}
}
void addToList(int data){
element* newNode=new element(data);
newNode->next=NULL;
element *tmp=head;
if (tmp!=NULL){
while(tmp->next!=NULL){
tmp=tmp->next;
}
tmp->next=newNode;
}
else{
head=newNode;
}
size++;
}
List& operator=(const List& lista){
if (&lista==this) return *this;
if (this->head!=NULL){
element* tmp=head;
element *temp;
while(tmp!=NULL){
temp=tmp->next;
delete tmp;
tmp=temp;
}
head=NULL;
}
if (lista.size==0){
size=0;
head=NULL;
}
else
{
size=0;
head=NULL;
element* tmp=lista.head;
while(tmp!=NULL){
this->addToList(tmp->number);
tmp=tmp->next;
}
}
return *this;
}
List operator+(const List &lista){
List newList;
element *tmp=this->head;
while (tmp!=NULL){
newList.addToList(tmp->number);
tmp=tmp->next;
}
tmp=lista.head;
while (tmp!=NULL){
newList.addToList(tmp->number);
tmp=tmp->next;
}
return newList;
}
};
istream & operator>>(istream &str, List &lists){
int data;
str >> data;
lists.addToList(data);
return str;
}
ostream & operator<<(ostream &str, List &lists){
element*tmp=lists.head;
if (tmp==NULL){
str << "List is empty" << endl;
}
else{
while(tmp!=NULL){
str << tmp->number << "--";
tmp=tmp->next;
}
str << "NULL" << endl << "Size of lsit: " << lists.size << endl;
}
return str;
}
int main(){
List lista;
cin >> lista;
cin >> lista;
List list2=lista;
cout << lista;
cout << list2;
List c;
//cout << list2+lista; CANT DO THAT, WHY?
c=list2+lista;
cout << c;
return 0;
}
#DrewDormann Here is error:
lista.cpp: In function ‘int main()’:
lista.cpp:167:7: error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘List’)
cout << lista+list2;
^
lista.cpp:167:7: note: candidates are:
In file included from /usr/include/c++/4.8/iostream:39:0,
from lista.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 ‘List’ 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 ‘List’ 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 ‘List’ 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 ‘List’ 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 ‘List’ 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 ‘List’ to ‘bool’
In file included from /usr/include/c++/4.8/ostream:609:0,
from /usr/include/c++/4.8/iostream:39,
from lista.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 ‘List’ to ‘short int’
In file included from /usr/include/c++/4.8/iostream:39:0,
from lista.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 ‘List’ 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 lista.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 ‘List’ to ‘int’
In file included from /usr/include/c++/4.8/iostream:39:0,
from lista.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 ‘List’ 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 ‘List’ 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 ‘List’ 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 ‘List’ 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 ‘List’ 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 ‘List’ 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 ‘List’ to ‘const void*’
In file included from /usr/include/c++/4.8/ostream:609:0,
from /usr/include/c++/4.8/iostream:39,
from lista.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 ‘List’ to ‘std::basic_ostream<char>::__streambuf_type* {aka std::basic_streambuf<char>*}’
lista.cpp:141:12: note: std::ostream& operator<<(std::ostream&, List&)
ostream & operator<<(ostream &str, List &lists){
^
lista.cpp:141:12: note: no known conversion for argument 2 from ‘List’ to ‘List&’
In file included from /usr/include/c++/4.8/iostream:39:0,
from lista.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:
lista.cpp:167:16: note: cannot convert ‘List::operator+(const List&)((*(const List*)(& list2)))’ (type ‘List’) to type ‘const unsigned char*’
cout << lista+list2;
^
In file included from /usr/include/c++/4.8/iostream:39:0,
from lista.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:
lista.cpp:167:16: note: cannot convert ‘List::operator+(const List&)((*(const List*)(& list2)))’ (type ‘List’) to type ‘const signed char*’
cout << lista+list2;
^
In file included from /usr/include/c++/4.8/iostream:39:0,
from lista.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:
lista.cpp:167:16: note: cannot convert ‘List::operator+(const List&)((*(const List*)(& list2)))’ (type ‘List’) to type ‘const char*’
cout << lista+list2;
^
In file included from /usr/include/c++/4.8/ostream:609:0,
from /usr/include/c++/4.8/iostream:39,
from lista.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:
lista.cpp:167:16: note: cannot convert ‘List::operator+(const List&)((*(const List*)(& list2)))’ (type ‘List’) to type ‘const char*’
cout << lista+list2;
^
In file included from /usr/include/c++/4.8/iostream:39:0,
from lista.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: template argument deduction/substitution failed:
lista.cpp:167:16: note: mismatched types ‘const _CharT*’ and ‘List’
cout << lista+list2;
^
In file included from /usr/include/c++/4.8/iostream:39:0,
from lista.cpp:1:
/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:
lista.cpp:167:16: note: cannot convert ‘List::operator+(const List&)((*(const List*)(& list2)))’ (type ‘List’) to type ‘unsigned char’
cout << lista+list2;
^
In file included from /usr/include/c++/4.8/iostream:39:0,
from lista.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:
lista.cpp:167:16: note: cannot convert ‘List::operator+(const List&)((*(const List*)(& list2)))’ (type ‘List’) to type ‘signed char’
cout << lista+list2;
^
In file included from /usr/include/c++/4.8/iostream:39:0,
from lista.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:
lista.cpp:167:16: note: cannot convert ‘List::operator+(const List&)((*(const List*)(& list2)))’ (type ‘List’) to type ‘char’
cout << lista+list2;
^
In file included from /usr/include/c++/4.8/iostream:39:0,
from lista.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:
lista.cpp:167:16: note: cannot convert ‘List::operator+(const List&)((*(const List*)(& list2)))’ (type ‘List’) to type ‘char’
cout << lista+list2;
^
In file included from /usr/include/c++/4.8/iostream:39:0,
from lista.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: template argument deduction/substitution failed:
lista.cpp:167:16: note: deduced conflicting types for parameter ‘_CharT’ (‘char’ and ‘List’)
cout << lista+list2;
^
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 lista.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:
lista.cpp:167:16: note: ‘List’ is not derived from ‘const std::basic_string<_CharT, _Traits, _Alloc>’
cout << lista+list2;
I know that this is when i'm trying to use operaotr without overloading, but here problem is that operator << is overloaded.
Yes, it is possible.
For example: Imagine that both + and = operators also invert a number. If you invert twice - you get correct result. But if you invert it only once (e.g. using non-overloaded = operator) you get an unexpected result.
What is happening in your case - I have no idea. You provide not enough data for a more concrete and useful answer.

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<<")";}
// ^^^^^
// ...
};