Home > Back-end >  Compiler error at line 30 when trying to output vector
Compiler error at line 30 when trying to output vector

Time:12-14

I'm just starting to take C courses online and right now, I'm doing vectors. Can someone please tell me what I did wrong on line 30 and 31? I can't seem to output what is in vector_2d at index 0 and 1. Thanks!

# include <iostream>
# include <vector>
using namespace std;

int main()
{
    vector <int> vector1 {};
    vector <int> vector2 {};

    vector1.push_back(10);
    vector1.push_back(20);

    cout << vector1.at(0) << endl;
    cout << vector1.at(1) << endl;
    cout << vector1.size() << endl;



    vector2.push_back(100);
    vector2.push_back(200);

    cout << vector2.at(0) << endl;
    cout << vector2.at(1) << endl;

    vector <vector <int>> vector_2d {};

    vector_2d.push_back(vector1);
    vector_2d.push_back(vector2);

    cout << vector_2d.at(0) << endl;
    cout << vector_2d.at(1) << endl;
    
    return 0;
}

Update, I've copied and pasted below most of the complete error output, I'm limited to 30,000 characters. :

C:\Users\FT86TT\AppData\Local\JetBrains\CLion2021.2\cygwin_cmake\bin\cmake.exe --build "/cygdrive/c/Users/FT86TT/Desktop/COP 3363/untitled14/cmake-build-debug" --target untitled14 -- -j 6
Scanning dependencies of target untitled14
[ 50%] Building CXX object CMakeFiles/untitled14.dir/main.cpp.o
/cygdrive/c/Users/FT86TT/Desktop/COP 3363/untitled14/main.cpp: In function 'int main()':
/cygdrive/c/Users/FT86TT/Desktop/COP 3363/untitled14/main.cpp:32:10: 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> >, std::vector<int> >::value_type' {aka 'std::vector<int>'})
   32 |     cout << vector_2d.at(0) << endl;
      |     ~~~~ ^~ ~~~~~~~~~~~~~~~
      |     |                   |
      |     |                   __gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, std::vector<int> >::value_type {aka std::vector<int>}
      |     std::ostream {aka std::basic_ostream<char>}
In file included from /usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /iostream:39,
                 from /cygdrive/c/Users/FT86TT/Desktop/COP 3363/untitled14/main.cpp:1:
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /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>]'
  108 |       operator<<(__ostream_type& (*__pf)(__ostream_type&))
      |       ^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /ostream:108:36: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, 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>&)'}
  108 |       operator<<(__ostream_type& (*__pf)(__ostream_type&))
      |                  ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /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>]'
  117 |       operator<<(__ios_type& (*__pf)(__ios_type&))
      |       ^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /ostream:117:32: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, 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>&)'}
  117 |       operator<<(__ios_type& (*__pf)(__ios_type&))
      |                  ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /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>]'
  127 |       operator<<(ios_base& (*__pf) (ios_base&))
      |       ^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /ostream:127:30: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, std::vector<int> >::value_type' {aka 'std::vector<int>'} to 'std::ios_base& (*)(std::ios_base&)'
  127 |       operator<<(ios_base& (*__pf) (ios_base&))
      |                  ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /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>]'
  166 |       operator<<(long __n)
      |       ^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /ostream:166:23: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, std::vector<int> >::value_type' {aka 'std::vector<int>'} to 'long int'
  166 |       operator<<(long __n)
      |                  ~~~~~^~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /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>]'
  170 |       operator<<(unsigned long __n)
      |       ^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /ostream:170:32: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, std::vector<int> >::value_type' {aka 'std::vector<int>'} to 'long unsigned int'
  170 |       operator<<(unsigned long __n)
      |                  ~~~~~~~~~~~~~~^~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /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>]'
  174 |       operator<<(bool __n)
      |       ^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /ostream:174:23: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, std::vector<int> >::value_type' {aka 'std::vector<int>'} to 'bool'
  174 |       operator<<(bool __n)
      |                  ~~~~~^~~
In file included from /usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /ostream:826,
                 from /usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /iostream:39,
                 from /cygdrive/c/Users/FT86TT/Desktop/COP 3363/untitled14/main.cpp:1:
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /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>]'
   91 |     basic_ostream<_CharT, _Traits>::
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /bits/ostream.tcc:92:22: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, std::vector<int> >::value_type' {aka 'std::vector<int>'} to 'short int'
   92 |     operator<<(short __n)
      |                ~~~~~~^~~
In file included from /usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /iostream:39,
                 from /cygdrive/c/Users/FT86TT/Desktop/COP 3363/untitled14/main.cpp:1:
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /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>]'
  181 |       operator<<(unsigned short __n)
      |       ^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /ostream:181:33: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, std::vector<int> >::value_type' {aka 'std::vector<int>'} to 'short unsigned int'
  181 |       operator<<(unsigned short __n)
      |                  ~~~~~~~~~~~~~~~^~~
In file included from /usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /ostream:826,
                 from /usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /iostream:39,
                 from /cygdrive/c/Users/FT86TT/Desktop/COP 3363/untitled14/main.cpp:1:
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /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>]'
  105 |     basic_ostream<_CharT, _Traits>::
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /bits/ostream.tcc:106:20: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, std::vector<int> >::value_type' {aka 'std::vector<int>'} to 'int'
  106 |     operator<<(int __n)
      |                ~~~~^~~
In file included from /usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /iostream:39,
                 from /cygdrive/c/Users/FT86TT/Desktop/COP 3363/untitled14/main.cpp:1:
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /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>]'
  192 |       operator<<(unsigned int __n)
      |       ^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /ostream:192:31: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, std::vector<int> >::value_type' {aka 'std::vector<int>'} to 'unsigned int'
  192 |       operator<<(unsigned int __n)
      |                  ~~~~~~~~~~~~~^~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /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>]'
  201 |       operator<<(long long __n)
      |       ^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /ostream:201:28: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, std::vector<int> >::value_type' {aka 'std::vector<int>'} to 'long long int'
  201 |       operator<<(long long __n)
      |                  ~~~~~~~~~~^~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /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>]'
  205 |       operator<<(unsigned long long __n)
      |       ^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /ostream:205:37: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, std::vector<int> >::value_type' {aka 'std::vector<int>'} to 'long long unsigned int'
  205 |       operator<<(unsigned long long __n)
      |                  ~~~~~~~~~~~~~~~~~~~^~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /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>]'
  220 |       operator<<(double __f)
      |       ^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /ostream:220:25: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, std::vector<int> >::value_type' {aka 'std::vector<int>'} to 'double'
  220 |       operator<<(double __f)
      |                  ~~~~~~~^~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /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>]'
  224 |       operator<<(float __f)
      |       ^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /ostream:224:24: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, std::vector<int> >::value_type' {aka 'std::vector<int>'} to 'float'
  224 |       operator<<(float __f)
      |                  ~~~~~~^~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /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>]'
  232 |       operator<<(long double __f)
      |       ^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /ostream:232:30: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, std::vector<int> >::value_type' {aka 'std::vector<int>'} to 'long double'
  232 |       operator<<(long double __f)
      |                  ~~~~~~~~~~~~^~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /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>]'
  245 |       operator<<(const void* __p)
      |       ^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /ostream:245:30: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, std::vector<int> >::value_type' {aka 'std::vector<int>'} to 'const void*'
  245 |       operator<<(const void* __p)
      |                  ~~~~~~~~~~~~^~~
In file included from /usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /ostream:826,
                 from /usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /iostream:39,
                 from /cygdrive/c/Users/FT86TT/Desktop/COP 3363/untitled14/main.cpp:1:
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /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>]'
  119 |     basic_ostream<_CharT, _Traits>::
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /bits/ostream.tcc:120:34: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, std::vector<int> >::value_type' {aka 'std::vector<int>'} to 'std::basic_ostream<char>::__streambuf_type*' {aka 'std::basic_streambuf<char>*'}
  120 |     operator<<(__streambuf_type* __sbin)
      |                ~~~~~~~~~~~~~~~~~~^~~~~~
In file included from /usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /string:55,
                 from /usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /bits/locale_classes.h:40,
                 from /usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /bits/ios_base.h:41,
                 from /usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /ios:42,
                 from /usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /ostream:38,
                 from /usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /iostream:39,
                 from /cygdrive/c/Users/FT86TT/Desktop/COP 3363/untitled14/main.cpp:1:
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /bits/basic_string.h:6530:5: note: candidate: '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>&)'
 6530 |     operator<<(basic_ostream<_CharT, _Traits>& __os,
      |     ^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /bits/basic_string.h:6530:5: note:   template argument deduction/substitution failed:
/cygdrive/c/Users/FT86TT/Desktop/COP 3363/untitled14/main.cpp:32:27: note:   '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, std::vector<int> >::value_type' {aka 'std::vector<int>'} is not derived from 'const std::basic_string<_CharT, _Traits, _Alloc>'
   32 |     cout << vector_2d.at(0) << endl;
      |                           ^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /bits/ios_base.h:46,
                 from /usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /ios:42,
                 from /usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /ostream:38,
                 from /usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /iostream:39,
                 from /cygdrive/c/Users/FT86TT/Desktop/COP 3363/untitled14/main.cpp:1:
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /system_error:263:5: note: candidate: 'template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::error_code&)'
  263 |     operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __e)
      |     ^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /system_error:263:5: note:   template argument deduction/substitution failed:
/cygdrive/c/Users/FT86TT/Desktop/COP 3363/untitled14/main.cpp:32:25: note:   cannot convert 'vector_2d.std::vector<std::vector<int> >::at(0)' (type '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, std::vector<int> >::value_type' {aka 'std::vector<int>'}) to type 'const std::error_code&'
   32 |     cout << vector_2d.at(0) << endl;
      |             ~~~~~~~~~~~~^~~
In file included from /usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /iostream:39,
                 from /cygdrive/c/Users/FT86TT/Desktop/COP 3363/untitled14/main.cpp:1:
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /ostream:506:5: note: candidate: 'template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, _CharT)'
  506 |     operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
      |     ^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c  /ostream:506:5: note:   template argument deduction/substitution failed:
/cygdrive/c/Users/FT86TT/Desktop/COP 3363/untitled14/main.cpp:32:27: note:   deduced conflicting types for parameter '_CharT' ('char' and 'std::vector<int>')
   32 |     cout << vector_2d.at(0) << endl;
      |                           ^

CodePudding user response:

The elements of the vector vector_2d are in turn objects of the type std::vector<int>.

vector <vector <int>> vector_2d {};

There is no standard overloaded operator << for vectors. So the compiler issues error messages for these statements

cout << vector_2d.at(0) << endl;
cout << vector_2d.at(1) << endl;

You could use the range based for loop to output elements of the vectors as for example

for ( const auto &item : vector_2d.at(0) )
{
    std::cout << item << ' ';
}
std::cout << '\n';

for ( const auto &item : vector_2d.at(1) )
{
    std::cout << item << ' ';
}
std::cout << '\n';

Otherwise you need to overload the operator << for the class std::vector<int> yourself as for example

$include <iostream>
#include <vector>

std::ostream &operator <<( std::ostream &os, const std::vector<int> &v )
{
    for (const auto &item : v)
    {
        os << item << ' ';
    }

    return os;
}

int main()
{
    std::vector<int> v = { 1, 2, 3, 4, 5 };

    std::cout << v << '\n';
}

And call the operator like

std::cout << vector_2d.at(0) << '\n';

Pay attention to that this initialization of the vectors

vector <int> vector1 {};
vector <int> vector2 {};

is redundant and has no effect. You could just write

vector <int> vector1;
vector <int> vector2;

In the both cases empty vectors are defined.

Bear in mind that for your program there is no great sense to use the member function at. You could use the subscript operator like

vector_2d[0]

Otherwise when you are using the member function at when it is better to enclose it in a try-catch block.

CodePudding user response:

vector_2d is of type std::vector<std::vector<int>>, so vector_2d.at(0) and vector_2d.at(1) is of type std::vector. Since std::cout don't know how to print an object of type std::vector<int>, it gives an error.

If you want to print the whole vector, do:

for(const auto &i : vector_2d.at(0))
{
    std::cout << i << ' ';
}
std::cout << '\n'
for(const auto &i : vector_2d.at(1))
{
    std::cout << i << ' ';
}

CodePudding user response:

I changed:

cout << vector_2d.at(0) << endl;
cout << vector_2d.at(1) << endl;

to:

cout << vector_2d.at(0).at(0) << " " << vector_2d.at(0).at(1) << endl;
cout << vector_2d.at(1).at(0) << " " << vector_2d.at(1).at(1) << endl;

Thanks all!

  • Related