1

I'm trying to make a program that converts text you put in to bigger ascii art text. I made a prototype and ran it, and I got this error

error: no match for 'operator<<' (operand types are 'std::ostream' {aka 'std::basic_ostream<char>'} and 'std::map<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> >::iterator' {aka 'std::_Rb_tree<std::__cxx11::basic_string<char>, std::pair<const std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> >, std::_Select1st<std::pair<const std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> > >, std::less<std::__cxx11::basic_string<char> >, std::allocator<std::pair<const std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> > > >::iterator'})

(The full error is way too large to put here)

I think it's because I'm using a very specific type that std::cout doesn't support. I've tried tons of different ways to convert it into a normal string, but all of the functions I've tried don't support it either.

Code:

#include <map>
#include <string>
#include <iostream>
#include <vector>
#include <bits/stdc++.h>

int main() {
    std::map<std::string, std::string> bigabc = {{"A", " |||||     \n||   ||   \n||||||| \n||   ||"}, {"B", "|||||||\n|     |\n||||||\n|     |\n|||||||"}, {"C", "|||||||\n||\n||\n||\n|||||||"}};

    std::string input;

    std::cin >> input;
    std::transform(input.begin(), input.end(), input.begin(), ::toupper);

    std::vector<std::string> chars(input.begin(), input.end());

    for (int pos = 0; pos < chars.size(); pos++) {
        std::cout << bigabc.find(chars[pos]) << "\n\n";
    }
}

I've been stuck on this for hours. Is there any way to make this work, or should I just rewrite my code entirely?

I tried using bigabc.at() instead and got this error:

In file included from C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/vector:66,
                 from asciisnake.cpp:4:
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_uninitialized.h: In instantiation of '_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >; _ForwardIterator = std::__cxx11::basic_string<char>*]':
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_uninitialized.h:325:37:   required from '_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >; _ForwardIterator = std::__cxx11::basic_string<char>*; _Tp = std::__cxx11::basic_string<char>]'
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_vector.h:1585:33:   required from 'void std::vector<_Tp, _Alloc>::_M_range_initialize(_ForwardIterator, _ForwardIterator, std::forward_iterator_tag) [with _ForwardIterator = __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >; _Tp = std::__cxx11::basic_string<char>; _Alloc = std::allocator<std::__cxx11::basic_string<char> >]'
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_vector.h:657:23:   required from 'std::vector<_Tp, _Alloc>::vector(_InputIterator, _InputIterator, const allocator_type&) [with _InputIterator = __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >; <template-parameter-2-2> = void; _Tp = std::__cxx11::basic_string<char>; _Alloc = std::allocator<std::__cxx11::basic_string<char> >; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::__cxx11::basic_string<char> >]'
asciisnake.cpp:15:62:   required from here
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_uninitialized.h:137:72: error: static assertion failed: result type must be constructible from value type of input range
  137 |       static_assert(is_constructible<_ValueType2, decltype(*__first)>::value,
      |

Tried using const map, <char, string> and bigabc[chars[pos]] and got this error:

asciisnake.cpp: In function 'int main()':
asciisnake.cpp:20:23: error: no match for 'operator[]' (operand types are 'const std::map<char, std::__cxx11::basic_string<char> >' and '__gnu_cxx::__alloc_traits<std::allocator<std::__cxx11::basic_string<char> >, std::__cxx11::basic_string<char> >::value_type' {aka 'std::__cxx11::basic_string<char>'})
   20 |         cout << bigabc[chars[pos]] << "\n\n";
      |                       ^
In file included from C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/map:61,
                 from asciisnake.cpp:1:
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_map.h:492:7: note: candidate: 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = char; _Tp = std::__cxx11::basic_string<char>; _Compare = std::less<char>; _Alloc = std::allocator<std::pair<const char, std::__cxx11::basic_string<char> > >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = std::__cxx11::basic_string<char>; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = char]'
  492 |       operator[](const key_type& __k)
      |       ^~~~~~~~
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_map.h:492:34: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::__cxx11::basic_string<char> >, std::__cxx11::basic_string<char> >::value_type' {aka 'std::__cxx11::basic_string<char>'} to 'const key_type&' {aka 'const char&'}
  492 |       operator[](const key_type& __k)
      |                  ~~~~~~~~~~~~~~~~^~~
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_map.h:512:7: note: candidate: 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](std::map<_Key, _Tp, _Compare, _Alloc>::key_type&&) [with _Key = char; _Tp = std::__cxx11::basic_string<char>; _Compare = std::less<char>; _Alloc = std::allocator<std::pair<const char, std::__cxx11::basic_string<char> > >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = std::__cxx11::basic_string<char>; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = char]'
  512 |       operator[](key_type&& __k)
      |       ^~~~~~~~
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_map.h:512:29: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::__cxx11::basic_string<char> >, std::__cxx11::basic_string<char> >::value_type' {aka 'std::__cxx11::basic_string<char>'} to 'std::map<char, std::__cxx11::basic_string<char> >::key_type&&' {aka 'char&&'}
  512 |       operator[](key_type&& __k)
      |                  ~~~~~~~~~~~^~~
In file included from C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/vector:66,
                 from asciisnake.cpp:4:
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_uninitialized.h: In instantiation of '_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >; _ForwardIterator = std::__cxx11::basic_string<char>*]':
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_uninitialized.h:325:37:   required from '_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >; _ForwardIterator = std::__cxx11::basic_string<char>*; _Tp = std::__cxx11::basic_string<char>]'
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_vector.h:1585:33:   required from 'void std::vector<_Tp, _Alloc>::_M_range_initialize(_ForwardIterator, _ForwardIterator, std::forward_iterator_tag) [with _ForwardIterator = __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >; _Tp = std::__cxx11::basic_string<char>; _Alloc = std::allocator<std::__cxx11::basic_string<char> >]'
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_vector.h:657:23:   required from 'std::vector<_Tp, _Alloc>::vector(_InputIterator, _InputIterator, const allocator_type&) [with _InputIterator = __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >; <template-parameter-2-2> = void; _Tp = std::__cxx11::basic_string<char>; _Alloc = std::allocator<std::__cxx11::basic_string<char> >; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::__cxx11::basic_string<char> >]'
asciisnake.cpp:17:52:   required from here
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_uninitialized.h:137:72: error: static assertion failed: result type must be constructible from value type of input range
  137 |       static_assert(is_constructible<_ValueType2, decltype(*__first)>::value,
      |
  • 2
    looks like you're trying to print the iterator, not the stuff in the iterator (which is a `std::pair` and still can't be printed without help) – user4581301 Jun 24 '22 at 02:00
  • 2
    In your own words, what do you expect `bigabc.find(chars[pos])` to return? What *type* do you expect it to have? What do you think it should *look like* when printed to `std::cout`? *Why*? (Also: *what do you think should happen* if `chars[pos]` *isn't* in the map?) – Karl Knechtel Jun 24 '22 at 02:01
  • If your compiler's not an antique, see if `bigabc.at(chars[pos])` does what you want. – user4581301 Jun 24 '22 at 02:01
  • 1
    [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – Retired Ninja Jun 24 '22 at 02:06
  • @user4581301 That gives a whole new error. I'll edit the question – Nicholas Picklas Jun 24 '22 at 02:06
  • Nicholas, it wasn't necessary to delete [you other question](https://stackoverflow.com/questions/74671803/c-sharp-file-not-recognizing-nuget-package), you could have just edited it. Also, if you think your problem is now solved based on any comments we might have made, either post an answer or let us know and we can post an answer. Either way, answers help others in the same predicament. :) –  Dec 04 '22 at 00:24

1 Answers1

1

bigabc.find() will return an iterator pointing to the matching pair {"A","||......"}. You can use ->second to get the second string, and that will print fine.
Even easier, you can use the [] operator instead of find(): cout << bigabc[chars[pos]]; will print the matching 'big' string for chars[pos].

Aganju
  • 6,295
  • 1
  • 12
  • 23
  • That gives me the same error as using bigabc.at() – Nicholas Picklas Jun 24 '22 at 02:14
  • Make your map `const`; otherwise it can't work with constant strings. `at()` and `[]` both try to create an entry if it is missing. – Aganju Jun 24 '22 at 02:24
  • Still not working. Updated the question with the new error. – Nicholas Picklas Jun 24 '22 at 02:29
  • But it's making progress! Your map should be `` (and then single quotes for the key: `{'A',"....."}`), as you are looking up a single char: `chars[pos]` is a char. Alternatively (but poorer) you could convert `chars[pos]` into a `std::string` before using it in the lookup. – Aganju Jun 24 '22 at 02:57
  • That gives me yet another different error, but I didn't know that using single quotes made a difference! I thought it did the same thing as in python. – Nicholas Picklas Jun 24 '22 at 03:03
  • @NicholasPicklas Please keep in mind that this is **not a discussion forum**. The goal here is to answer questions, not to go back and forth with you until the code works. If you have a completely different problem arising from changes to the code, please ask a new question. Also, [please try](https://meta.stackoverflow.com/questions/261592) to resolve each new problem yourself first as they come up, or at least to figure out what is going on and look for existing questions. It seems that you have inadvertently skipped quite a bit while learning the fundamentals. There is no way around that. – Karl Knechtel Jun 24 '22 at 05:50
  • @NicholasPicklas Python and C++ have about as much in common as English and Finnish. The fundamentals of programming are the same, but the way you get there... Wooo-eeee. Not the same at all. Don't forget everything you learned with Python, but you'll need to learn a new way. – user4581301 Jun 24 '22 at 07:22