The problem isn't the void *
, it's the std::string
. The callback is expecting a pointer but you're trying to pass a plain object.
...comments about why you should avoid using plain pointers in C++ aside.
Вопрос
I am porting some code over from C# into C++ and I am having trouble using the void*
to take the place of a generic object. I am trying to pass the undefined pointer as a parameter for a function but have not had an success.
Here is the code to my class:
class ReceiveRequest
{
typedef boost::function<void (std::string *message, ssError& e, void *payload) > receiveCallback;
public:
receiveCallback Callback;
void* payload;
ReceiveRequest(receiveCallback _Callback, void* _payload)
{
Callback = _Callback;
payload = _payload;
}
void runCallback (std::string message, ssError e)
{
Callback(message, e, this->payload);
}
~ReceiveRequest() { }
};
And here are the errors I receive:
In file included from StringSocket.h:16,
from StringSocket.cpp:8:
ReceiveRequest.h: In member function ‘void ReceiveRequest::runCallback(std::string, ssError)’:
ReceiveRequest.h:30: error: no match for call to ‘(ReceiveRequest::receiveCallback) (std::string&, ssError&, void*&)’
/home/jsander/boost/include/boost/function/function_template.hpp:761: note: candidates are: R boost::function3<R, T1, T2, T3>::operator()(T0, T1, T2) const [with R = void, T0 = std::string*, T1 = ssError&, T2 = void*]
Why does my void*
variable become a void*&
when I pass it as a parameter? To fix this should I just change the typedef
function to take a void*&
to fix my problem?
Решение
The problem isn't the void *
, it's the std::string
. The callback is expecting a pointer but you're trying to pass a plain object.
...comments about why you should avoid using plain pointers in C++ aside.