M
M
MasterCopipaster2021-06-14 19:08:35
C++ / C#
MasterCopipaster, 2021-06-14 19:08:35

Why is there a "cannot be implicitly captured because no default capture mode has been specified" error when passed to a function?

Why is there an error when I write this code?

bool f(char* buf, char* key_input) {
boost::beast::string_view key = std::string(key_input);
//todo 
        ws.set_option(websocket::stream_base::decorator([](websocket::request_type& req) {
            req.set("Api-Key", key);
            })
        );
//todo

C:\Project2.cpp(40,1): error C3493: 'key' cannot be implicitly captured because no default capture mode has been specified
C:\Project2.cpp(39,56): error C2440: '': cannot convert from 'int' to 'boost::beast::websocket::stream_base::decorator'
C:\Project2.cpp(41,14): message : No constructor could take the source type, or constructor overload resolution was ambiguous
Done building project "Project2.vcxproj" -- FAILED.


But if I write like this, then everything is ok
ws.set_option(websocket::stream_base::decorator([](websocket::request_type& req) {
            req.set("Api-Key", "28369a57-7476-4d2c-9b9d-574d4282b428");
            })
        );

According to the definition of the set function
template<class Allocator>
void
basic_fields<Allocator>::
set(field name, string_view const& value)


or
template<class Allocator>
void
basic_fields<Allocator>::
set(string_view sname, string_view const& value)


Why is this happening and how to fix it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2021-06-14
@MasterCopipaster

Read about Lambdas. The error message clearly tells you that it can't figure out what to do with the key variable because no default capture method was specified and key was not listed in the capture list.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question