Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help for a Beginner? #2141

Closed
duojet2ez opened this issue May 26, 2020 · 4 comments
Closed

Help for a Beginner? #2141

duojet2ez opened this issue May 26, 2020 · 4 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@duojet2ez
Copy link

duojet2ez commented May 26, 2020

Hello,

I am trying to make a text based game and had the idea to use a json file to store all of the text for each page. And then I can conveniently turn the text into a json object and in my main.cpp output the text from the json file.

Because I am new (barely figured out how to get this library installed) I am having a hell of a time understanding the documentation. Is my stated objective possible?

The only thing I have figured out how to do is store text in a json file:

{
"text": "goes here"

}

and then in my main.cpp file I call that text by opening and reading into the file:

 std::ifstream inFile("pagetest.json");  
json j; 
inFile >> j; 
std::string s = j.dump(); 
std::cout << s << std::endl; 

However, this does not behave as I'd expect. It outputs everything including the quotations and the {} which is what I don't want. I would need just a string outputted.

Basically I am very confused.... Any help would be greatly appreciated.

@nlohmann
Copy link
Owner

If you only need the value at key "text", do this:

std::cout << j["text"] << std::endl;

If you do not want the quotes (i.e., a serialize JSON value of type string), but a std::string, do this:

std::string s = j["text"];
std::cout << s << std::endl;

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label May 26, 2020
@duojet2ez
Copy link
Author

Fantastic that worked!!! So simple! Thank you for answering. I am slowly understanding the power of json.

@nonetrix
Copy link

nonetrix commented Jul 11, 2021

This worked but I tried to do something like this
std::string title = reddit_result[0]["data"]["children"][0]["data"]["title"] + reddit_result[0]["data"]["children"][0]["data"]["url_overridden_by_dest"];

But that does this:

./main.cpp: In function ‘int main()’:
./main.cpp:52:82: error: no match for ‘operator+’ (operand types are ‘nlohmann::basic_json<>::value_type’ {aka ‘nlohmann::basic_json<>’} and ‘nlohmann::basic_json<>::value_type’ {aka ‘nlohmann::basic_json<>’})
   52 | title = reddit_result[0]["data"]["children"][0]["data"]["title"] + reddit_result[0]["data"]["children"][0]["data"]["url_overridden_by_dest"];

In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/stl_algobase.h:67,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/algorithm:61,
                 from ././json.hpp:37,
                 from ./main.cpp:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/stl_iterator.h:1239:5: note: candidate: ‘template<class _Iterator, class _Container> __gnu_cxx::__normal_iterator<_Iterator, _Container> __gnu_cxx::operator+(typename __gnu_cxx::__normal_iterator<_Iterator, _Container>::difference_type, const __gnu_cxx::__normal_iterator<_Iterator, _Container>&)’
 1239 |     operator+(typename __normal_iterator<_Iterator, _Container>::difference_type
      |     ^~~~~~~~
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/stl_iterator.h:1239:5: note:   template argument deduction/substitution failed:
./main.cpp:52:156: note:   ‘nlohmann::basic_json<>::value_type’ {aka ‘nlohmann::basic_json<>’} is not derived from ‘const __gnu_cxx::__normal_iterator<_Iterator, _Container>’
   52 | + reddit_result[0]["data"]["children"][0]["data"]["url_overridden_by_dest"];
      |                                                                           ^

In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/stl_algobase.h:67,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/algorithm:61,
                 from ././json.hpp:37,
                 from ./main.cpp:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/stl_iterator.h:565:5: note: candidate: ‘template<class _Iterator> constexpr std::reverse_iterator<_Iterator> std::operator+(typename std::reverse_iterator<_Iterator>::difference_type, const std::reverse_iterator<_Iterator>&)’
  565 |     operator+(typename reverse_iterator<_Iterator>::difference_type __n,
      |     ^~~~~~~~
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/stl_iterator.h:565:5: note:   template argument deduction/substitution failed:
./main.cpp:52:156: note:   ‘nlohmann::basic_json<>::value_type’ {aka ‘nlohmann::basic_json<>’} is not derived from ‘const std::reverse_iterator<_Iterator>’
   52 | + reddit_result[0]["data"]["children"][0]["data"]["url_overridden_by_dest"];
      |                                                                           ^

In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/stl_algobase.h:67,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/algorithm:61,
                 from ././json.hpp:37,
                 from ./main.cpp:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/stl_iterator.h:1646:5: note: candidate: ‘template<class _Iterator> constexpr std::move_iterator<_IteratorL> std::operator+(typename std::move_iterator<_IteratorL>::difference_type, const std::move_iterator<_IteratorL>&)’
 1646 |     operator+(typename move_iterator<_Iterator>::difference_type __n,
      |     ^~~~~~~~
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/stl_iterator.h:1646:5: note:   template argument deduction/substitution failed:
./main.cpp:52:156: note:   ‘nlohmann::basic_json<>::value_type’ {aka ‘nlohmann::basic_json<>’} is not derived from ‘const std::move_iterator<_IteratorL>’
   52 | + reddit_result[0]["data"]["children"][0]["data"]["url_overridden_by_dest"];
      |                                                                           ^

In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/string:55,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/locale_classes.h:40,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/ios_base.h:41,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/streambuf:41,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/streambuf_iterator.h:35,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/iterator:66,
                 from ././json.hpp:42,
                 from ./main.cpp:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/basic_string.h:6081:5: note: candidate: ‘template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>&, const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>&)’
 6081 |     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
      |     ^~~~~~~~
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/basic_string.h:6081:5: note:   template argument deduction/substitution failed:
./main.cpp:52:156: note:   ‘nlohmann::basic_json<>::value_type’ {aka ‘nlohmann::basic_json<>’} is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>’
   52 | + reddit_result[0]["data"]["children"][0]["data"]["url_overridden_by_dest"];
      |                                                                           ^

In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/string:56,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/locale_classes.h:40,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/ios_base.h:41,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/streambuf:41,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/streambuf_iterator.h:35,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/iterator:66,
                 from ././json.hpp:42,
                 from ./main.cpp:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/basic_string.tcc:1169:5  note: candidate: ‘template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const _CharT*, const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>&)’
 1169 |     operator+(const _CharT* __lhs,
      |     ^~~~~~~~
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/basic_string.tcc:1169:5  note:   template argument deduction/substitution failed:
./main.cpp:52:156: note:   mismatched types ‘const _CharT*’ and ‘nlohmann::basic_json<>’
   52 | + reddit_result[0]["data"]["children"][0]["data"]["url_overridden_by_dest"];
      |                                                                           ^

In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/string:56,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/locale_classes.h:40,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/ios_base.h:41,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/streambuf:41,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/streambuf_iterator.h:35,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/iterator:66,
                 from ././json.hpp:42,
                 from ./main.cpp:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/basic_string.tcc:1189:5  note: candidate: ‘template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(_CharT, const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>&)’
 1189 |     operator+(_CharT __lhs, const basic_string<_CharT, _Traits, _Alloc>& __rhs)
      |     ^~~~~~~~
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/basic_string.tcc:1189:5  note:   template argument deduction/substitution failed:
./main.cpp:52:156: note:   ‘nlohmann::basic_json<>::value_type’ {aka ‘nlohmann::basic_json<>’} is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>’
   52 | + reddit_result[0]["data"]["children"][0]["data"]["url_overridden_by_dest"];
      |                                                                           ^

In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/string:55,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/locale_classes.h:40,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/ios_base.h:41,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/streambuf:41,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/streambuf_iterator.h:35,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/iterator:66,
                 from ././json.hpp:42,
                 from ./main.cpp:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/basic_string.h:6118:5: note: candidate: ‘template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>&, const _CharT*)’
 6118 |     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
      |     ^~~~~~~~
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/basic_string.h:6118:5: note:   template argument deduction/substitution failed:
./main.cpp:52:156: note:   ‘nlohmann::basic_json<>::value_type’ {aka ‘nlohmann::basic_json<>’} is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>’
   52 | + reddit_result[0]["data"]["children"][0]["data"]["url_overridden_by_dest"];
      |                                                                           ^

In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/string:55,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/locale_classes.h:40,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/ios_base.h:41,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/streambuf:41,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/streambuf_iterator.h:35,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/iterator:66,
                 from ././json.hpp:42,
                 from ./main.cpp:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/basic_string.h:6134:5: note: candidate: ‘template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>&, _CharT)’
 6134 |     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
      |     ^~~~~~~~
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/basic_string.h:6134:5: note:   template argument deduction/substitution failed:
./main.cpp:52:156: note:   ‘nlohmann::basic_json<>::value_type’ {aka ‘nlohmann::basic_json<>’} is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>’
   52 | + reddit_result[0]["data"]["children"][0]["data"]["url_overridden_by_dest"];
      |                                                                           ^

In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/string:55,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/locale_classes.h:40,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/ios_base.h:41,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/streambuf:41,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/streambuf_iterator.h:35,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/iterator:66,
                 from ././json.hpp:42,
                 from ./main.cpp:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/basic_string.h:6146:5: note: candidate: ‘template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(std::__cxx11::basic_string<_CharT, _Traits, _Allocator>&&, const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>&)’
 6146 |     operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
      |     ^~~~~~~~
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/basic_string.h:6146:5: note:   template argument deduction/substitution failed:
./main.cpp:52:156: note:   ‘nlohmann::basic_json<>::value_type’ {aka ‘nlohmann::basic_json<>’} is not derived from ‘std::__cxx11::basic_string<_CharT, _Traits, _Allocator>’
   52 | + reddit_result[0]["data"]["children"][0]["data"]["url_overridden_by_dest"];
      |                                                                           ^

In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/string:55,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/locale_classes.h:40,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/ios_base.h:41,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/streambuf:41,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/streambuf_iterator.h:35,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/iterator:66,
                 from ././json.hpp:42,
                 from ./main.cpp:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/basic_string.h:6152:5: note: candidate: ‘template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>&, std::__cxx11::basic_string<_CharT, _Traits, _Allocator>&&)’
 6152 |     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
      |     ^~~~~~~~
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/basic_string.h:6152:5: note:   template argument deduction/substitution failed:
./main.cpp:52:156: note:   ‘nlohmann::basic_json<>::value_type’ {aka ‘nlohmann::basic_json<>’} is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>’
   52 | + reddit_result[0]["data"]["children"][0]["data"]["url_overridden_by_dest"];
      |                                                                           ^

In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/string:55,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/locale_classes.h:40,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/ios_base.h:41,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/streambuf:41,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/streambuf_iterator.h:35,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/iterator:66,
                 from ././json.hpp:42,
                 from ./main.cpp:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/basic_string.h:6158:5: note: candidate: ‘template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(std::__cxx11::basic_string<_CharT, _Traits, _Allocator>&&, std::__cxx11::basic_string<_CharT, _Traits, _Allocator>&&)’
 6158 |     operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
      |     ^~~~~~~~
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/basic_string.h:6158:5: note:   template argument deduction/substitution failed:
./main.cpp:52:156: note:   ‘nlohmann::basic_json<>::value_type’ {aka ‘nlohmann::basic_json<>’} is not derived from ‘std::__cxx11::basic_string<_CharT, _Traits, _Allocator>’
   52 | + reddit_result[0]["data"]["children"][0]["data"]["url_overridden_by_dest"];
      |                                                                           ^

In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/string:55,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/locale_classes.h:40,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/ios_base.h:41,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/streambuf:41,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/streambuf_iterator.h:35,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/iterator:66,
                 from ././json.hpp:42,
                 from ./main.cpp:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/basic_string.h:6180:5: note: candidate: ‘template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const _CharT*, std::__cxx11::basic_string<_CharT, _Traits, _Allocator>&&)’
 6180 |     operator+(const _CharT* __lhs,
      |     ^~~~~~~~
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/basic_string.h:6180:5: note:   template argument deduction/substitution failed:
./main.cpp:52:156: note:   mismatched types ‘const _CharT*’ and ‘nlohmann::basic_json<>’
   52 | + reddit_result[0]["data"]["children"][0]["data"]["url_overridden_by_dest"];
      |                                                                           ^

In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/string:55,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/locale_classes.h:40,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/ios_base.h:41,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/streambuf:41,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/streambuf_iterator.h:35,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/iterator:66,
                 from ././json.hpp:42,
                 from ./main.cpp:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/basic_string.h:6186:5: note: candidate: ‘template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(_CharT, std::__cxx11::basic_string<_CharT, _Traits, _Allocator>&&)’
 6186 |     operator+(_CharT __lhs,
      |     ^~~~~~~~
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/basic_string.h:6186:5: note:   template argument deduction/substitution failed:
./main.cpp:52:156: note:   ‘nlohmann::basic_json<>::value_type’ {aka ‘nlohmann::basic_json<>’} is not derived from ‘std::__cxx11::basic_string<_CharT, _Traits, _Allocator>’
   52 | + reddit_result[0]["data"]["children"][0]["data"]["url_overridden_by_dest"];
      |                                                                           ^

In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/string:55,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/locale_classes.h:40,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/ios_base.h:41,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/streambuf:41,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/streambuf_iterator.h:35,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/iterator:66,
                 from ././json.hpp:42,
                 from ./main.cpp:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/basic_string.h:6192:5: note: candidate: ‘template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(std::__cxx11::basic_string<_CharT, _Traits, _Allocator>&&, const _CharT*)’
 6192 |     operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
      |     ^~~~~~~~
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/basic_string.h:6192:5: note:   template argument deduction/substitution failed:
./main.cpp:52:156: note:   ‘nlohmann::basic_json<>::value_type’ {aka ‘nlohmann::basic_json<>’} is not derived from ‘std::__cxx11::basic_string<_CharT, _Traits, _Allocator>’
   52 | + reddit_result[0]["data"]["children"][0]["data"]["url_overridden_by_dest"];
      |                                                                           ^

In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/string:55,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/locale_classes.h:40,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/ios_base.h:41,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/streambuf:41,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/streambuf_iterator.h:35,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/iterator:66,
                 from ././json.hpp:42,
                 from ./main.cpp:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/basic_string.h:6198:5: note: candidate: ‘template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(std::__cxx11::basic_string<_CharT, _Traits, _Allocator>&&, _CharT)’
 6198 |     operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
      |     ^~~~~~~~
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/basic_string.h:6198:5: note:   template argument deduction/substitution failed:
./main.cpp:52:156: note:   ‘nlohmann::basic_json<>::value_type’ {aka ‘nlohmann::basic_json<>’} is not derived from ‘std::__cxx11::basic_string<_CharT, _Traits, _Allocator>’
   52 | + reddit_result[0]["data"]["children"][0]["data"]["url_overridden_by_dest"];
      |                                                                           ^

In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/valarray:603,
                 from ././json.hpp:67,
                 from ./main.cpp:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/valarray_after.h:405:5: note: candidate: ‘template<class _Dom1, class _Dom2> std::_Expr<std::__detail::_BinClos<std::__plus, std::_Expr, std::_Expr, _Dom1, _Dom2>, typename std::__fun<std::__plus, typename _Dom1::value_type>::result_type> std::operator+(const std::_Expr<_Dom1, typename _Dom1::value_type>&, const std::_Expr<_Dom2, typename _Dom2::value_type>&)’
  405 |     _DEFINE_EXPR_BINARY_OPERATOR(+, struct std::__plus)
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/valarray_after.h:405:5: note:   template argument deduction/substitution failed:
./main.cpp:52:156: note:   ‘nlohmann::basic_json<>::value_type’ {aka ‘nlohmann::basic_json<>’} is not derived from ‘const std::_Expr<_Dom1, typename _Dom1::value_type>’
   52 | + reddit_result[0]["data"]["children"][0]["data"]["url_overridden_by_dest"];
      |                                                                           ^

In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/valarray:603,
                 from ././json.hpp:67,
                 from ./main.cpp:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/valarray_after.h:405:5: note: candidate: ‘template<class _Dom> std::_Expr<std::__detail::_BinClos<std::__plus, std::_Expr, std::_Constant, _Dom, typename _Dom::value_type>, typename std::__fun<std::__plus, typename _Dom1::value_type>::result_type> std::operator+(const std::_Expr<_Dom1, typename _Dom1::value_type>&, const typename _Dom::value_type&)’
  405 |     _DEFINE_EXPR_BINARY_OPERATOR(+, struct std::__plus)
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/valarray_after.h:405:5: note:   template argument deduction/substitution failed:
./main.cpp:52:156: note:   ‘nlohmann::basic_json<>::value_type’ {aka ‘nlohmann::basic_json<>’} is not derived from ‘const std::_Expr<_Dom1, typename _Dom1::value_type>’
   52 | + reddit_result[0]["data"]["children"][0]["data"]["url_overridden_by_dest"];
      |                                                                           ^

In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/valarray:603,
                 from ././json.hpp:67,
                 from ./main.cpp:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/valarray_after.h:405:5: note: candidate: ‘template<class _Dom> std::_Expr<std::__detail::_BinClos<std::__plus, std::_Constant, std::_Expr, typename _Dom::value_type, _Dom>, typename std::__fun<std::__plus, typename _Dom1::value_type>::result_type> std::operator+(const typename _Dom::value_type&, const std::_Expr<_Dom1, typename _Dom1::value_type>&)’
  405 |     _DEFINE_EXPR_BINARY_OPERATOR(+, struct std::__plus)
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/valarray_after.h:405:5: note:   template argument deduction/substitution failed:
./main.cpp:52:156: note:   ‘nlohmann::basic_json<>::value_type’ {aka ‘nlohmann::basic_json<>’} is not derived from ‘const std::_Expr<_Dom1, typename _Dom1::value_type>’
   52 | + reddit_result[0]["data"]["children"][0]["data"]["url_overridden_by_dest"];
      |                                                                           ^

In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/valarray:603,
                 from ././json.hpp:67,
                 from ./main.cpp:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/valarray_after.h:405:5: note: candidate: ‘template<class _Dom> std::_Expr<std::__detail::_BinClos<std::__plus, std::_Expr, std::_ValArray, _Dom, typename _Dom::value_type>, typename std::__fun<std::__plus, typename _Dom1::value_type>::result_type> std::operator+(const std::_Expr<_Dom1, typename _Dom1::value_type>&, const std::valarray<typename _Dom::value_type>&)’
  405 |     _DEFINE_EXPR_BINARY_OPERATOR(+, struct std::__plus)
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/valarray_after.h:405:5: note:   template argument deduction/substitution failed:
./main.cpp:52:156: note:   ‘nlohmann::basic_json<>::value_type’ {aka ‘nlohmann::basic_json<>’} is not derived from ‘const std::_Expr<_Dom1, typename _Dom1::value_type>’
   52 | + reddit_result[0]["data"]["children"][0]["data"]["url_overridden_by_dest"];
      |                                                                           ^

In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/valarray:603,
                 from ././json.hpp:67,
                 from ./main.cpp:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/valarray_after.h:405:5: note: candidate: ‘template<class _Dom> std::_Expr<std::__detail::_BinClos<std::__plus, std::_ValArray, std::_Expr, typename _Dom::value_type, _Dom>, typename std::__fun<std::__plus, typename _Dom1::value_type>::result_type> std::operator+(const std::valarray<typename _Dom::value_type>&, const std::_Expr<_Dom1, typename _Dom1::value_type>&)’
  405 |     _DEFINE_EXPR_BINARY_OPERATOR(+, struct std::__plus)
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/bits/valarray_after.h:405:5: note:   template argument deduction/substitution failed:
./main.cpp:52:156: note:   ‘nlohmann::basic_json<>::value_type’ {aka ‘nlohmann::basic_json<>’} is not derived from ‘const std::_Expr<_Dom1, typename _Dom1::value_type>’
   52 | + reddit_result[0]["data"]["children"][0]["data"]["url_overridden_by_dest"];
      |                                                                           ^

In file included from ././json.hpp:67,
                 from ./main.cpp:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/valarray:1185:1: note: candidate: ‘template<class _Tp> std::_Expr<std::__detail::_BinClos<std::__plus, std::_ValArray, std::_ValArray, _Tp, _Tp>, typename std::__fun<std::__plus, _Tp>::result_type> std::operator+(const std::valarray<_Tp>&, const std::valarray<_Tp>&)’
 1185 | _DEFINE_BINARY_OPERATOR(+, __plus)
      | ^~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/valarray:1185:1: note:   template argument deduction/substitution failed:
./main.cpp:52:156: note:   ‘nlohmann::basic_json<>::value_type’ {aka ‘nlohmann::basic_json<>’} is not derived from ‘const std::valarray<_Tp>’
   52 | + reddit_result[0]["data"]["children"][0]["data"]["url_overridden_by_dest"];
      |                                                                           ^

In file included from ././json.hpp:67,
                 from ./main.cpp:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/valarray:1185:1: note: candidate: ‘template<class _Tp> std::_Expr<std::__detail::_BinClos<std::__plus, std::_ValArray, std::_Constant, _Tp, _Tp>, typename std::__fun<std::__plus, _Tp>::result_type> std::operator+(const std::valarray<_Tp>&, const typename std::valarray<_Tp>::value_type&)’
 1185 | _DEFINE_BINARY_OPERATOR(+, __plus)
      | ^~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/valarray:1185:1: note:   template argument deduction/substitution failed:
./main.cpp:52:156: note:   ‘nlohmann::basic_json<>::value_type’ {aka ‘nlohmann::basic_json<>’} is not derived from ‘const std::valarray<_Tp>’
   52 | + reddit_result[0]["data"]["children"][0]["data"]["url_overridden_by_dest"];
      |                                                                           ^

In file included from ././json.hpp:67,
                 from ./main.cpp:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/valarray:1185:1: note: candidate: ‘template<class _Tp> std::_Expr<std::__detail::_BinClos<std::__plus, std::_Constant, std::_ValArray, _Tp, _Tp>, typename std::__fun<std::__plus, _Tp>::result_type> std::operator+(const typename std::valarray<_Tp>::value_type&, const std::valarray<_Tp>&)’
 1185 | _DEFINE_BINARY_OPERATOR(+, __plus)
      | ^~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11/valarray:1185:1: note:   template argument deduction/substitution failed:
./main.cpp:52:156: note:   ‘nlohmann::basic_json<>::value_type’ {aka ‘nlohmann::basic_json<>’} is not derived from ‘const std::valarray<_Tp>’
   52 | + reddit_result[0]["data"]["children"][0]["data"]["url_overridden_by_dest"];
      |                                                                           ^

Also is there a better way to do this? allocating memory just to print something without quotation marks doesn't seem ideal

@gregmarr
Copy link
Contributor

@nonetrix reddit_result[0]["data"]["children"][0]["data"]["title"] is of type nlohmann::json, you need to get the actual string from it to combine it with another string. You can do that without copying using the get_ref() function. I think it would be written as get_ref<const std::string&>() but I haven't compiled and tested to be sure.
https://nlohmann.github.io/json/api/basic_json/get_ref/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

4 participants