Understanding Error C2664: Cannot convert argument 1 - c++

The following code snippet
#include "uWS/uWS.h"
int main()
{
uWS::Hub h;
h.onConnection([&h](uWS::WebSocket<uWS::SERVER> *ws, uWS::HttpRequest req) {
std::cout << "Connected!!!" << std::endl;
});
h.run();
}
Generates the errors
Severity Code Description Project File Line Suppression State Error
(active) E0304 no instance of overloaded function
"uWS::Hub::onConnection" matches the argument
list pid c:\Users\R\src\main.cpp 6 Error C2664 'void
uWS::Group<false>::onConnection(std::function<void
(uWS::WebSocket<false>,uWS::HttpRequest)>)': cannot convert argument 1
from 'main::<lambda_1afdd040d2f03ded23f0c636dc85475d>' to
'std::function<void
(uWS::WebSocket<true>,uWS::HttpRequest)>' pid c:\users\r\src\main.cpp 8
When built in Visual Studio 2017 IDE using Windows SDK Version 10.0.15063.0,
where "uWS/uWS.h" contains the tiny web sockets definitions
What could be the problem?

From the error message, it looks like the onConnection function expects a function which takes a uws::WebSocket but your lambda function accepts a uws::WebSocket* instead.

Related

How can I fix the argument type conversion compiler errors in this method using a lambda function?

I am programming for the ESP32 (a sort of Arduino like chip). However, to easier/faster find compiler errors/warnings and later make a sort of virtualization on the PC, I like to compile the code also on a PC (using Visual Studio).
However, I cannot get the following code to compile on a PC (while it compiles in the Arduino IDE for ESP32):
server.on("/", HTTP_GET, [](AsyncWebServerRequest* request)
{
request->send_P(200, "text/html", textBuffer, Processor);
});
The errors I get are:
Severity Code Description Project File Line Suppression State
Error (active) E0413 no suitable conversion function from "lambda []void (AsyncWebServerRequest *request)->void" to "AsyncWebServerRequest *" exists RelayBox C:\Users\miche\source\repos\RelayBox2\RelayBox\RelayBox\RelayBoxServer.cpp 203
Error C2664 'void AsyncWebServer::on(const char *,int,AsyncWebServerRequest *)': cannot convert argument 3 from 'RelayBoxServer::Send::<lambda_6ffcff35888ca3a1f1d7d541e1edeba3>' to 'AsyncWebServerRequest *' RelayBox C:\Users\miche\source\repos\RelayBox2\RelayBox\RelayBox\RelayBoxServer.cpp 206
The code is based on the ESP32 library at https://github.com/me-no-dev/ESPAsyncWebServer/blob/master/src/ESPAsyncWebServer.h
And the 'stub' classes I created using library above but simplified for my project, with '...' as irrelevant code:
class AsyncWebServer
{
...
void on(const char* something, int httpGet, AsyncWebServerRequest* function);
...
}
#define HTTP_GET 100
class AsyncWebServerRequest
{
public:
void send_P(int port, const char* textHmlt, STRING text, ProcessorHandlerFunction function);
};
class ArduinoStringStub : public std::string
{
...
}
How can I fix the argument type conversion compiler errors in this method using a lambda function?
I've checked your reference github, it seems that the origin AsyncWebServer class has such signature:
AsyncCallbackWebHandler& on(const char* uri, WebRequestMethodComposite method, ArRequestHandlerFunction onRequest);
And ArRequestHandlerFunction is actually:
typedef std::function<void(AsyncWebServerRequest *request)> ArRequestHandlerFunction;
So it seems to be that the stub/mock AsyncWebServer you create should not take AsyncWebServerRequest* but ArRequestHandlerFunction as the on method's last parameter, corresponding with the original code.

wxWidgets Drop Files eventhandler initialization problem (invalid static_cast)

I am trying to make a simple program to drop files on a list using C++ with a (wxWidgets) wxListCtrl.
Tools I use: Code::Blocks 20.03, MinGW 17.1, wxWidgets 3.1.3, wxFormBuilder 3.9.0.
OS: Windows 10 Pro.
I tried the following: (in a function SetupGUI() which is called in the DnD_SimpleFrame class constructor.)
m_listCtrl1 = new wxListCtrl( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_NO_HEADER|wxLC_REPORT );
m_listCtrl1->DragAcceptFiles(true);
m_listCtrl1->Connect(wxEVT_DROP_FILES, wxDropFilesEventHandler(DnD_SimpleFrame::OnDropFiles), NULL, this);
The function to be called on dropping the files (from explorer) is:
bool DnD_SimpleFrame::OnDropFiles(wxArrayString &filenames)
{
size_t nFiles = filenames.GetCount();
wxString str;
str.Printf( "%d files dropped", (int)nFiles);
m_listCtrl1->DeleteAllItems();
if (m_listCtrl1 != NULL)
{
m_listCtrl1->InsertItem(0, str);
for ( size_t n = 1; n < (nFiles+1); n++ )
m_listCtrl1->InsertItem(n, filenames[n]);
}
return true;
}
The build messages on the m_listCtrl1->Connect(...); line are:
||=== Build: Debug in DnD_Simple (compiler: GNU GCC Compiler) ===|
F:\Data\__C++\wxApps\DnD_Simple\DnD_SimpleMain.cpp||In member function 'void DnD_SimpleFrame::SetupGUI()':|
F:\SDKs\wx313\include\wx\event.h|149|error: invalid static_cast from type 'bool (DnD_SimpleFrame::*)(wxArrayString&)' to type 'wxDropFilesEventFunction' {aka 'void (wxEvtHandler::*)(wxDropFilesEvent&)'}|
F:\SDKs\wx313\include\wx\event.h|4196|note: in expansion of macro 'wxEVENT_HANDLER_CAST'|
F:\Data\__C++\wxApps\DnD_Simple\DnD_SimpleMain.cpp|92|note: in expansion of macro 'wxDropFilesEventHandler'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|
What am I doing wrong here (or am forgetting)?
If you carefully look at the error message:
error: invalid static_cast from type 'bool (DnD_SimpleFrame::*)(wxArrayString&)' to type 'wxDropFilesEventFunction' {aka 'void (wxEvtHandler::*)(wxDropFilesEvent&)'}
you can see that it says that it can't convert something from one type to the other one.
The types may be difficult to parse if you're new to C++, but you should be able to see that these are function pointers (in fact, pointers to a class member). And looking at your code, you should also be able to see that the first is them is the type of your DnD_SimpleFrame::OnDropFiles function and hence the problem is that it can't be converted to the expected function type.
Finally, the reason for this is just that your function doesn't have the right parameter type: you're supposed to give wxWidgets something taking wxDropFilesEvent&, but your function takes wxArrayString& instead. You will have to change it to take wxDropFilesEvent& event and then use GetFiles() method of the event object to get the actual files.
On a completely different topic, you should use Bind() and not Connect() in the new code. If you're following a tutorial which uses the latter, it's a good sign that it's very outdated. Your code still wouldn't have compiled with Bind(), but the error messages would have been slightly simpler.

Template deduction error

Note: pls don't be confused with other Stackoverflow questions that have the same title/errocode -- they are mostly due to missing #include <string> but it's not the issue here.
I'm reading Pretty-print C++ STL containers question and downloaed Kerrek's code from his GitHub Project cxx-prettyprint, when I try compile it (my environment is Windows 7, Visual Studio 2013, compiled project as a Console Application "Use Multi-Byte Character Set"), hit a template deduction error.
It's a bit weird as suggested by Mike Kinghan in the comment, it does compile compiles and runs fine with the current MSVC++ : see it online.
I'm confused, there must be something wrong with my configuration. I created a blank new Windows Console Application, and added Kerrek's code in, focus on a vector of string test cast, removed other scenarios such as set/map etc:
int main(int argc, char* argv[])
{
std::vector<std::string> v;
v.push_back("hello, world");
v.push_back("2nd line");
v.push_back("last line");
std::cout << v;
}
, now hit a error:
Error 1 error C2679: binary '<<' : no operator found which takes a
right-hand operand of type
'std::vector>' (or there is no
acceptable conversion)
If I explicitly call the pretty_print::operator <<,
pretty_print::operator<<(std::cout, v);
, hit another template deduction error:
Error 1 error C2784: 'std::basic_ostream<_Elem,_Traits>
&pretty_print::operator <<(std::basic_ostream<_Elem,_Traits> &,const
pretty_print::print_container_helper
&)' : could not deduce template argument for 'const
pretty_print::print_container_helper
&' from 'std::vector>'
It only compiles if the print_container_helper is explictily created:
pretty_print::print_container_helper<std::vector<std::string>, char, ::std::char_traits<char>, pretty_print::delimiters<std::vector<std::string>, char>>
vh(v);
pretty_print::operator<<(std::cout, vh);
The full code is here: http://rextester.com/RJX76690

error C2664: 'imgGrab' : cannot convert parameter 2 from 'Int8 **' to 'void **'

I am writing an x64 DLL using Visual Studio 2008.
The compiler error
error C2664: 'imgGrab' : cannot convert parameter 2 from 'Int8 **' to 'void **'
occurs at this line;
Int8* ImaqBuffer;
Int32 err = imgGrab (Sid, &ImaqBuffer, TRUE);
I've managed to get it to compile by doing;
Int32 err = imgGrab (Sid, (void **)&ImaqBuffer, TRUE);
but I think this is wrong. Am I right in thinking this is wrong? If so how do I fix it. I suspect its a compiler setting in Visual Studio because the code compiles ok in the sample project from the manufacturer.

Boost bind inside Boost packaged_task. Why boost asio thinks its not CompletionHandler?

So all my work happens inside of a class named thread_pool. This code will work no matter what run_item takes into itself:
template <class task_return_t>
void thread_pool::pool_item( boost::shared_ptr< boost::packaged_task<task_return_t> > pt)
{
internal_tasks.post(boost::bind(&thread_pool::run_item<task_return_t>, this, pt));
//...
This will not compile:
template <class task_return_t>
void thread_pool::pool_item( boost::shared_ptr< boost::packaged_task<task_return_t> > pt)
{
boost::packaged_task<void> task ( boost::bind(&thread_pool::run_item<task_return_t>, this, pt)));
internal_tasks.post( task);
Why? And how to make it compile?
I use boost 1.47.0. Errors my VS2010 talls me:
Error 6 error C2665: 'boost::asio::detail::zero_arg_handler_test' : none of the 2 overloads could convert all the argument types C:\Program Files (x86)\Boost-1.47.0\include\boost\asio\impl\io_service.hpp 88 1 cf-server
Error 9 error C2664: 'void boost::asio::detail::task_io_service::post<CompletionHandler>(Handler)' : cannot convert parameter 1 from 'const boost::packaged_task<R>' to 'boost::packaged_task<R>' C:\Program Files (x86)\Boost-1.47.0\include\boost\asio\impl\io_service.hpp 90 1 cf-server
Error 8 error C2664: 'T &boost::asio::detail::lvref<CompletionHandler>(T)' : cannot convert parameter 1 from 'const boost::packaged_task<R>' to 'boost::packaged_task<R>' C:\Program Files (x86)\Boost-1.47.0\include\boost\asio\impl\io_service.hpp 88 1 cf-server
Error 7 error C2664: 'boost::asio::detail::clvref' : cannot convert parameter 1 from 'const boost::packaged_task<R>' to 'boost::packaged_task<R>' C:\Program Files (x86)\Boost-1.47.0\include\boost\asio\impl\io_service.hpp 88 1 cf-server
It looks like the function thread_pool::run_item<task_return_t> accepts a non-const packaged task reference (or pointer) but boost::bind made a const copy, and is unable to pass the argument.
Without seeing the run_item signature, I can't tell exactly, but that's where I would look.
Edit: Looking a bit deeper (once I could get to a copy of boost 1_47), the problem is that boost::packaged_tasks are not copyable, however, boost::asio::io_service requires that CompletionHandler be CopyConstructable. Since boost::packaged_tasks are only MoveConstructable/MoveAssignable, you cannot pass them directly to the io_service.
See Boost.Asio requirements for CompletionHandlers