我正在将一些代码从C#移植到C ++中,而我在使用void*
取代通用对象时遇到了麻烦。我试图将未定义的指针作为函数的参数传递但是没有成功。
以下是我班级的代码:
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() { }
};
以下是我收到的错误:
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*]
为什么我的void*
变量在作为参数传递时变为void*&
?为了解决这个问题,我应该更改typedef
功能,以便void*&
解决我的问题吗?
答案 0 :(得分:4)
问题不是void *
,而是std::string
。回调期待一个指针,但你试图传递一个普通的对象。
...评论为什么你应该避免在C ++中使用普通指针。